The Wayback Machine - https://web.archive.org/web/20240921070340/https://www.geeksforgeeks.org/recursive-program-to-print-multiplication-table-of-a-number/
Open In App

Recursive Program to print multiplication table of a number

Last Updated : 28 Feb, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a number N, the task is to print its multiplication table using recursion. 
Examples 
 

Input: N = 5 
Output: 
5 * 1 = 5 
5 * 2 = 10 
5 * 3 = 15 
5 * 4 = 20 
5 * 5 = 25 
5 * 6 = 30 
5 * 7 = 35 
5 * 8 = 40 
5 * 9 = 45 
5 * 10 = 50

Input: N = 8 
Output: 
8 * 1 = 8 
8 * 2 = 16 
8 * 3 = 24 
8 * 4 = 32 
8 * 5 = 40 
8 * 6 = 48 
8 * 7 = 56 
8 * 8 = 64 
8 * 9 = 72 
8 * 10 = 80 
 

 

 

Recursive approach to print multiplication table of a number

Approach: 
 

  1. Get the number for which multiplication table is to print.
  2. Recursively iterate from value 1 to 10: 
    • Base case: If the value called recursively is greater than 10, exit from the function. 
       
if(i > N) 
  return ;
  • Recursive call: If the base case is not met, then print its multiplication table for that value and then call the function for next iteration. 
     
print("N*i = ", N*i)
recursive_function(N, i+1);
  • Return statement: At each recursive call(except the base case), return the recursive function for next iteration. 
     
return recursive_function(N, i+1); 

Below is the implementation of the above approach:
 

C++




// C++ program to print table
// of a number using recursion
#include <iostream>
using namespace std;
 
// Function that print the
// table of a given number
// using recursion
void mul_table(int N, int i)
{
    // Base Case
    if (i > 10)
        return;
 
    // Print the table for
    // current iteration
    cout << N << " * " << i
         << " = " << N * i
         << endl;
 
    // Recursive call to next
    // iteration
    return mul_table(N, i + 1);
}
 
// Driver Code
int main()
{
    // Input number whose table
    // is to print
    int N = 8;
 
    // Function call to print
    // the table
    mul_table(N, 1);
    return 0;
}


Java




// Java program to print table
// of a number using recursion
class GFG {
     
    // Function that print the
    // table of a given number
    // using recursion
    static void mul_table(int N, int i)
    {
        // Base Case
        if (i > 10)
            return ;
     
        // Print the table for
        // current iteration
        System.out.println(N + " * " + i + " = " + N * i);
                
        // Recursive call to next
        // iteration
        mul_table(N, i + 1);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        // Input number whose table
        // is to print
        int N = 8;
     
        // Function call to print
        // the table
        mul_table(N, 1);
    }
}
 
// This code is contributed by AnkitRai01


Python3




# Python3 program to print table
# of a number using recursion
 
# Function that print the
# table of a given number
# using recursion
def mul_table(N, i):
     
    # Base Case
    if (i > 10):
        return
     
    # Print the table for
    # current iteration
    print(N,"*",i,"=",N * i)
     
    # Recursive call to next
    # iteration
    return mul_table(N, i + 1)
 
# Driver Code
 
# Input number whose table
# is to print
N = 8
 
# Function call to print
# the table
mul_table(N, 1)
 
# This is contributed by shubhamsingh10


C#




// C# program to print table
// of a number using recursion
using System;
 
class GFG{
     
    // Function that print the
    // table of a given number
    // using recursion
    static void mul_table(int N, int i)
    {
        // Base Case
        if (i > 10)
            return ;
     
        // Print the table for
        // current iteration
        Console.WriteLine(N + " * " + i + " = " + N * i);
                
        // Recursive call to next
        // iteration
        mul_table(N, i + 1);
    }
     
    // Driver Code
    public static void Main()
    {
        // Input number whose table
        // is to print
        int N = 8;
     
        // Function call to print
        // the table
        mul_table(N, 1);
    }
}
 
// This code is contributed by AnkitRai01


Javascript




<script>
 
// Javascript program to print table
// of a number using recursion
 
// Function that print the
// table of a given number
// using recursion
function mul_table(N, i)
{
    // Base Case
    if (i > 10)
        return;
 
    // Print the table for
    // current iteration
    document.write(N + " * " + i
         + " = " + N * i
         + "<br>");
 
    // Recursive call to next
    // iteration
    return mul_table(N, i + 1);
}
 
// Driver Code
 
// Input number whose table
// is to print
var N = 8;
 
// Function call to print
// the table
mul_table(N, 1);
 
</script>


Output: 

8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80

 

Time Complexity: O(1)
Auxiliary Space: O(N) where n is recursion stack space.
 



Similar Reads

Program to print multiplication table of a number
Given a number n as input, we need to print its table. Examples : Input : 5 Output : 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50 Input : 8 Output : 8 * 1 = 8 8 * 2 = 16 8 * 3 = 24 8 * 4 = 32 8 * 5 = 40 8 * 6 = 48 8 * 7 = 56 8 * 8 = 64 8 * 9 = 72 8 * 10 = 80 8 * 11 = 88 8 * 12 = 96 Ex
9 min read
Multiplication table till N rows where every Kth row is table of K upto Kth term
Given a number N, the task is to print N rows where every Kth row consists of the multiplication table of K up to Kth term. Examples: Input: N = 3 Output: 1 2 4 3 6 9 Explanation: In the above series, in every Kth row, multiplication table of K upto K terms is printed. Input: N = 5 Output: 1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 Approach: The idea is t
5 min read
Matrix Multiplication | Recursive
Given two matrices A and B. The task is to multiply matrix A and matrix B recursively. If matrix A and matrix B are not multiplicative compatible, then generate output "Not Possible". Examples : Input: A = 12 56 45 78 B = 2 6 5 8Output: 304 520 480 894Input: A = 1 2 3 4 5 6 7 8 9 B = 1 2 3 4 5 6 7 8 9Output: 30 36 42 66 81 96 102 126 150 It is reco
9 min read
Kth Smallest Number in Multiplication Table
Given three integers M, N, and K. Consider a grid of M * N, where mat[i][j] = i*j (1 based index). The task is to return the Kth smallest element in the M*N multiplication table. Examples: Input: M = 3, N = 3, K = 5Output: 3Explanation: The 5th smallest element is 3.  Input: M = 2, N = 3, K = 6Output: 6 Explanation: [1, 2, 3][2, 4, 6]. The 6th smal
14 min read
Recursive program to print formula for GCD of n integers
Given a function gcd(a, b) to find GCD (Greatest Common Divisor) of two number. It is also known that GCD of three elements can be found by gcd(a, gcd(b, c)), similarly for four element it can find the GCD by gcd(a, gcd(b, gcd(c, d))). Given a positive integer n. The task is to print the formula to find the GCD of n integer using given gcd() functi
4 min read
Recursive program to print all subsets with given sum
Given an array and a number, print all subsets with sum equal to given the sum.Examples: Input : arr[] = {2, 5, 8, 4, 6, 11}, sum = 13 Output : 5 8 2 11 2 5 6 Input : arr[] = {1, 5, 8, 4, 6, 11}, sum = 9 Output : 5 4 1 8 This problem is an extension of check if there is a subset with given sum. We recursively generate all subsets. We keep track of
6 min read
Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
Given a binary tree, the task is to print nodes of extreme corners of each level but in alternate order.Examples: Input : 1 / \ 2 3 / / \ 4 5 6 / / \ 7 8 9 Output : 1 2 6 7 Print the rightmost node at 1st level: 1 Print the leftmost node at 2nd level: 2 Print the rightmost node at 3rd level: 6 Print the leftmost node at 4th level: 7 Other possible
14 min read
CSES Solutions - Multiplication Table
Find the middle element when the numbers in an n × n multiplication table are sorted in increasing order. It is assumed that n is odd. For example, the 3 × 3 multiplication table is as follows: 1 2 32 4 63 6 9 The numbers in increasing order are [1,2,2,3,3,4,6,6,9], so the answer is 3 Example:Input: n = 3Output: 3Explanation: The 3x3 multiplication
4 min read
Print 1 to 100 without loop using Goto and Recursive-main
Our task is to print all numbers from 1 to 100 without using a loop. There are many ways to print numbers from 1 to 100 without using a loop. Two of them are the goto statement and the recursive main. Print numbers from 1 to 100 Using Goto statement Follow the steps mentioned below to implement the goto statement: declare variable i of value 0decla
5 min read
Print the last k nodes of the linked list in reverse order | Recursive approach
Given a linked list containing N nodes and a positive integer k should be less than or equal to N. The task is to print the last k nodes of the list in reverse order. Examples: Input: list: 1-&gt;2-&gt;3-&gt;4-&gt;5, k = 2 Output: 5 4 Input: list: 3-&gt;10-&gt;6-&gt;9-&gt;12-&gt;2-&gt;8, k = 4 Output: 8 2 12 9 Source: Amazon Interview Experience SD
7 min read
Recursive program for prime number
Given a number n, check whether it's prime number or not using recursion.Examples: Input : n = 11 Output : Yes Input : n = 15 Output : No The idea is based on school method to check for prime numbers. C/C++ Code // CPP Program to find whether a Number // is Prime or Not using Recursion #include &lt;bits/stdc++.h&gt; using namespace std; // Returns
4 min read
Recursive Program to find Factorial of a large number
Given a large number N, the task is to find the factorial of N using recursion. Factorial of a non-negative integer is the multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720. Examples: Input : N = 100Output : 933262154439441526816992388562667004-907159682643816214685929638952175999-9322
6 min read
Recursive program to find the Sum of the series 1 - 1/2 + 1/3 - 1/4 ... 1/N
Given a positive integer N, the task is to find the sum of the series 1 - (1/2) + (1/3) - (1/4) +.... (1/N) using recursion. Examples: Input: N = 3 Output: 0.8333333333333333 Explanation: 1 - (1/2) + (1/3) = 0.8333333333333333 Input: N = 4 Output: 0.5833333333333333 Explanation: 1- (1/2) + (1/3) - (1/4) = 0.5833333333333333 Approach: The idea is to
4 min read
Program for average of an array (Iterative and Recursive)
Given an array, the task is to find average of that array. Average is the sum of array elements divided by the number of elements. Examples : Input : arr[] = {1, 2, 3, 4, 5} Output : 3 Sum of the elements is 1+2+3+4+5 = 15 and total number of elements is 5. So average is 15/5 = 3 Input : arr[] = {5, 3, 6, 7, 5, 3} Output : 4.83333 Sum of the elemen
7 min read
Program to count vowels in a string (Iterative and Recursive)
Given a string, count the total number of vowels (a, e, i, o, u) in it. There are two methods to count total number of vowels in a string. Iterative Recursive Examples: Input : abc de Output : 2 Input : geeksforgeeks portal Output : 7Recommended PracticeVowel or NotTry It! 1. Iterative Method: Below is the implementation: C/C++ Code // C++ program
7 min read
Time complexity of recursive Fibonacci program
The Fibonacci numbers are the numbers in the following integer sequence 0, 1, 1, 2, 3, 5, 8, 13... Mathematically Fibonacci numbers can be written by the following recursive formula. For seed values F(0) = 0 and F(1) = 1F(n) = F(n-1) + F(n-2)Before proceeding with this article make sure you are familiar with the recursive approach discussed in Prog
2 min read
Non-recursive program to delete an entire binary tree
We have discussed recursive implementation to delete an entire binary tree here.We strongly recommend you to minimize your browser and try this yourself first.Now how to delete an entire tree without using recursion. Method 1: Level Order Tree Traversal. The idea is for each dequeued node from the queue, delete it after queuing its left and right n
10 min read
Program to check if an array is sorted or not (Iterative and Recursive)
Given an array of size n, write a program to check if it is sorted in ascending order or not. Equal values are allowed in an array and two consecutive equal values are considered sorted. Examples: Input : 20 21 45 89 89 90Output : YesInput : 20 20 45 89 89 90Output : YesInput : 20 20 78 98 99 97Output : NoApproach 1: Recursive approachThe basic ide
9 min read
Javascript Program For Recursive Selection Sort For Singly Linked List - Swapping Node Links
Given a singly linked list containing n nodes. The problem is to sort the list using the recursive selection sort technique. The approach should be such that it involves swapping node links instead of swapping node data. Examples: Input: 10 -&gt; 12 -&gt; 8 -&gt; 4 -&gt; 6Output: 4 -&gt; 6 -&gt; 8 -&gt; 10 -&gt; 12 In Selection Sort, we first find
4 min read
Recursive program to generate power set
Given a set represented as a string, write a recursive code to print all subsets of it. The subsets can be printed in any order. Examples: Input : set = "abc"Output : { "", "a", "b", "c", "ab", "ac", "bc", "abc"} Input : set = "abcd"Output : { "", "a" ,"ab" ,"abc" ,"abcd", "abd" ,"ac" ,"acd", "ad" ,"b", "bc" ,"bcd" ,"bd" ,"c" ,"cd" ,"d" } Using Pic
12 min read
Add 1 to number represented as array | Recursive Approach
Given an array which represents a number, add 1 to the array. Suppose an array contain elements [1, 2, 3, 4] then the array represents decimal number 1234 and hence adding 1 to this would result 1235. So new array will be [1, 2, 3, 5]. Examples: Input : [1, 2, 3, 4] Output : [1, 2, 3, 5] Input : [1, 2, 9, 9] Output : [1, 3, 0, 0] Input: [9, 9, 9, 9
6 min read
Recursive sum of digits of a number formed by repeated appends
Given two positive number N and X. The task is to find the sum of digits of a number formed by N repeating X number of times until sum become single digit. Examples : Input : N = 24, X = 3 Output : 9 Number formed after repeating 24 three time = 242424 Sum = 2 + 4 + 2 + 4 + 2 + 4 = 18 Sum is not the single digit, so finding the sum of digits of 18,
5 min read
Floor value Kth root of a number using Recursive Binary Search
Given two numbers N and K, the task is to find the floor value of Kth root of the number N. The Floor Kth root of a number N is the greatest whole number which is less than or equal to its Kth root.Examples: Input: N = 27, K = 3 Output: 3 Explanation: Kth root of 27 = 3. Therefore 3 is the greatest whole number less than equal to Kth root of 27. In
10 min read
Check if a number is magic (Recursive sum of digits is 1)
A number is said to be a magic number, if the sum of its digits are calculated till a single digit recursively by adding the sum of the digits after every addition. If the single digit comes out to be 1,then the number is a magic number. For example- Number= 50113 =&gt; 5+0+1+1+3=10 =&gt; 1+0=1 This is a Magic Number For example- Number= 1234 =&gt;
7 min read
Recursive sum of digits of a number is prime or not
Given a number n, we need to find the sum of each digits of the number till the number becomes a single digit. We need to print "yes" if the sum is a prime or "no" if it is not prime. Examples: Input : 5602 Output: No Explanation: Step 1- 5+6+0+2 = 13 Step 2- 1+3 = 4 4 is not prime Input : 56 Output : Yes Explanation: Step 1- 5+6 = 11 Step 2- 1+1 =
5 min read
Program for scalar multiplication of a matrix
Given a matrix and a scalar element k, our task is to find out the scalar product of that matrix. Examples: Input : mat[][] = {{2, 3} {5, 4}} k = 5 Output : 10 15 25 20 We multiply 5 with every element. Input : 1 2 3 4 5 6 7 8 9 k = 4 Output : 4 8 12 16 20 24 28 32 36 The scalar multiplication of a number k(scalar), multiply it on every entry in th
6 min read
Program for multiplication of array elements
We are given an array, and we have to calculate the product of an array using both iterative and recursive methods. Examples: Input : array[] = {1, 2, 3, 4, 5, 6}Output : 720Here, product of elements = 1*2*3*4*5*6 = 720 Input : array[] = {1, 3, 5, 7, 9}Output : 945 Iterative Method: We initialize result as 1. We traverse array from left to right an
9 min read
Sum of multiplication of triplet of divisors of a number
Given an array arr[] of integers of size n. For each element, you have to print the summation of multiplication of every triplet formed using divisors of this element.Examples: Input: arr[] = {4} Output: 8 4 has three divisors 1, 2 and 4. 1 * 2 * 4 = 8Input: arr[] = {9, 5, 6} Output: 27 0 72 9 has three divisors 1, 3 and 9. 1 * 3 * 9 = 27 5 has two
13 min read
Find minimum number K such that sum of array after multiplication by K exceed S
Given an array arr[] of N elements and an integer S, the task is to find the minimum number K such that the sum of the array elements does not exceed S after multiplying all the elements by K.Examples: Input: arr[] = { 1 }, S = 50 Output: 51 Explanation: The sum of array elements is 1. Now the multiplication of 1 with 51 gives 51 which is &gt; 50.
4 min read
Minimum division by 10 and multiplication by 2 required to reduce given number to 1
Given an integer N, the task is to reduce N to 1 by a minimum number of operations involving multiplication by 2 and division by 10. If 1 cannot be obtained, then print "-1". Examples: Input: N = 5Output: 2Explanation:Below are the operations performed:1st operation: Multiply N by 2. Therefore, N = 5 * 2 = 10.2nd operation: Divide N by 10. Therefor
5 min read
Article Tags :
Practice Tags :