Python Program to Multiply Two Numbers Using Recursion

In the previous article, we have discussed Python Program to Find Subtraction of Two Numbers using Recursion

Given two numbers and the task is to find the multiplication of the given two numbers using recursion.

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 First Number      =  3
Given Second Number =  5

Output:

The Multiplication of { 3 * 5 } using recursion = 15

Example2:

Input:

Given First Number      = 6
Given Second Number = 9

Output:

The Multiplication of { 6 * 9 } using recursion = 54

Program to Multiply Two Numbers Using RecursionĀ in Python

Below are the ways to find the multiplication of the given two numbers using recursion :

Method #1: Using Recursion (Static Input)

Approach:

  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the given two numbers as the arguments to recur_mult function.
  • Create a recursive function to say recur_mult which takes the two numbers as arguments and returns the multiplication of the given two numbers using recursion.
  • Check if the first number is less than the second number using the if conditional statement.
  • If the statement is true, then return the second number, first number as arguments to the recur_mult.{Recursive logic}.
  • Check if the second number is not equal to 0 using the elif conditional statement.
  • If the statement is true, then return fst_numb + recur_mult(fst_numb, secnd_numb – 1) {Recursive Logic}.
  • Else return 0.
  • Print the multiplication of the given two numbers using recursion.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say recur_mult which takes the two numbers as arguments
# and returns the multiplication of the given two numbers using recursion.


def recur_mult(fst_numb, secnd_numb):
  # Check if the first number is less than the second number using the if conditional
  # statement.
    if fst_numb < secnd_numb:
        # If the statement is true, then return the second number, first number as arguments
        # to the recur_mult.{Recursive logic}.
        return recur_mult(secnd_numb, fst_numb)
    # Check if the second number is not equal to 0 using the elif conditional statement.
    elif secnd_numb != 0:
        # If the statement is true, then return fst_numb + recur_mult(fst_numb, secnd_numb - 1)
        # {Recursive Logic}.

        return fst_numb + recur_mult(fst_numb, secnd_numb - 1)
    else:
        # Else return 0.
        return 0


# Give the first number as static input and store it in a variable.
fst_numb = 5
# Give the second number as static input and store it in another variable.
secnd_numb = 3
# Pass the given two numbers as the arguments to recur_mult function.
# Print the multiplication of given two numbers using recursion.
print("The Multiplication of {", fst_numb, "*", secnd_numb,
      "} using recursion =", recur_mult(fst_numb, secnd_numb))

Output:

The Multiplication of { 5 * 3 } using recursion = 15

Method #2: Using Recursion (User Input)

Approach:

  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the given two numbers as the arguments to recur_mult function.
  • Create a recursive function to say recur_mult which takes the two numbers as arguments and returns the multiplication of the given two numbers using recursion.
  • Check if the first number is less than the second number using the if conditional statement.
  • If the statement is true, then return the second number, first number as arguments to the recur_mult.{Recursive logic}.
  • Check if the second number is not equal to 0 using the elif conditional statement.
  • If the statement is true, then return fst_numb + recur_mult(fst_numb, secnd_numb – 1) {Recursive Logic}.
  • Else return 0.
  • Print the multiplication of the given two numbers using recursion.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say recur_mult which takes the two numbers as arguments
# and returns the multiplication of the given two numbers using recursion.


def recur_mult(fst_numb, secnd_numb):
  # Check if the first number is less than the second number using the if conditional
  # statement.
    if fst_numb < secnd_numb:
        # If the statement is true, then return the second number, first number as arguments
        # to the recur_mult.{Recursive logic}.
        return recur_mult(secnd_numb, fst_numb)
    # Check if the second number is not equal to 0 using the elif conditional statement.
    elif secnd_numb != 0:
        # If the statement is true, then return fst_numb + recur_mult(fst_numb, secnd_numb - 1)
        # {Recursive Logic}.

        return fst_numb + recur_mult(fst_numb, secnd_numb - 1)
    else:
        # Else return 0.
        return 0


# Give the first number as user input using the int(input()) function and 
# store it in a variable.
fst_numb = int(input("Enter some random number = "))
#Give the second number as user input using the int(input()) function and 
# store it in another variable.
secnd_numb = int(input("Enter some random number = "))
# Pass the given two numbers as the arguments to recur_mult function.
# Print the multiplication of given two numbers using recursion.
print("The Multiplication of {", fst_numb, "*", secnd_numb,
      "} using recursion =", recur_mult(fst_numb, secnd_numb))

Output:

Enter some random number = 7
Enter some random number = 0
The Multiplication of { 7 * 0 } using recursion = 0

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.