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 = 50Input: 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:
- Get the number for which multiplication table is to print.
- Recursively iterate from value 1 to 10:
-
Base case: If the value called recursively is greater than 10, exit from the function.
-
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.
Recommended Articles