Recursive Program to print multiplication table of a number

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.