Python Program to Print Multiplication Table using Recursion

In the previous article, we have discussed Python Program to Find Sum of Even Numbers Using Recursion in a List/Array

Given a number and the task is to print the multiplication table of that number using recursion in python.

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Examples:

Example1:

Input:

Given Number = 5

Output:

The Multiplication Table of the above given number  5 :
5  X  1  =  5
5  X  2  =  10
5  X  3  =  15
5  X  4  =  20
5  X  5  =  25
5  X  6  =  30
5  X  7  =  35
5  X  8  =  40
5  X  9  =  45
5  X  10  =  50

Example2:

Input:

Given Number = 2

Output:

The Multiplication Table of the above given number  2 :
2  X  1  =  2
2  X  2  =  4
2  X  3  =  6
2  X  4  =  8
2  X  5  =  10
2  X  6  =  12
2  X  7  =  14
2  X  8  =  16
2  X  9  =  18
2  X  10  =  20

Program to Print Multiplication Table using Recursion in Python

Below are the ways to print the multiplication table of the given number using recursion in python:

Method #1: Using Recursion (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Pass the given number and 1 as the arguments to the Mult_table_num function.
  • Create a recursive function to say Mult_table_num which takes the given number and k as the arguments and returns the multiplication table of the given number using recursion.
  • Print the given number multiplied by k.
  • Check if the k value is less than 10 using the if conditional statement.
  • If the statement is true, then pass the given number and k+1 value as the arguments to the Mult_table_num function.{Recursive Logic}
  • Print the multiplication table of the given number using recursion.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say Mult_table_num which takes the given number and k
# as the arguments and returns the multiplication table of the given number
# using recursion.


def Mult_table_num(gven_numb, k):
  # Print the given number multiplied by k.
    print(gven_numb, " X ", k, " = ", gven_numb * k)
    # Check if the k value is less than 10 using the if conditional statement.
    if (k < 10):
      # If the statement is true, then pass the given number and k+1 value as the arguments
      # to the Mult_table_num function.{Recursive Logic}
        Mult_table_num(gven_numb, k + 1)


# Give the number as static input and store it in a variable.
gven_numb = 5
# Print the multiplication table of the given number using recursion.
print("The Multiplication Table of the above given number ", gven_numb, ":")
# Pass the given number and 1 as the arguments to the Mult_table_num function.
Mult_table_num(gven_numb, 1)

Output:

The Multiplication Table of the above given number  5 :
5  X  1  =  5
5  X  2  =  10
5  X  3  =  15
5  X  4  =  20
5  X  5  =  25
5  X  6  =  30
5  X  7  =  35
5  X  8  =  40
5  X  9  =  45
5  X  10  =  50

Method #2: Using Recursion (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number and 1 as the arguments to the Mult_table_num function.
  • Create a recursive function to say Mult_table_num which takes the given number and k as the arguments and returns the multiplication table of the given number using recursion.
  • Print the given number multiplied by k.
  • Check if the k value is less than 10 using the if conditional statement.
  • If the statement is true, then pass the given number and k+1 value as the arguments to the Mult_table_num function.{Recursive Logic}
  • Print the multiplication table of the given number using recursion.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say Mult_table_num which takes the given number and k
# as the arguments and returns the multiplication table of the given number
# using recursion.


def Mult_table_num(gven_numb, k):
  # Print the given number multiplied by k.
    print(gven_numb, " X ", k, " = ", gven_numb * k)
    # Check if the k value is less than 10 using the if conditional statement.
    if (k < 10):
      # If the statement is true, then pass the given number and k+1 value as the arguments
      # to the Mult_table_num function.{Recursive Logic}
        Mult_table_num(gven_numb, k + 1)


# Give the number as user input using the int(input()) function and
# store it in a variable.
gven_numb = int(input("Enter some Random Number = "))
# Print the multiplication table of the given number using recursion.
print("The Multiplication Table of the above given number ", gven_numb, ":")
# Pass the given number and 1 as the arguments to the Mult_table_num function.
Mult_table_num(gven_numb, 1)

Output:

Enter some Random Number = 10
The Multiplication Table of the above given number 10 :
10 X 1 = 10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100

The best way to learn Python for Beginners is to practice as much as they can taking help of the Sample Python Programs For Beginners. Using them you can develop code on your own and master coding skills.