Python Program to Find the Product of two Numbers Using Recursion

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Recursion in Python:

When a function calls itself and loops until it reaches the intended end state, this is referred to as recursion. It is based on the mathematics idea of recursive definitions, which define elements in a set in terms of other members in the set.

Each recursive implementation contains a base case in which the desired state is reached, and a recursive case in which the desired state is not reached and the function enters another recursive phase.

On each step, the behavior in the recursive situation before the recursive function call, the internal self-call, is repeated. Recursive structures are beneficial when a larger problem (the base case) can be solved by solving repeated subproblems (the recursive case) that incrementally advance the program to the base case.
It behaves similarly to for and while loops, with the exception that recursion moves closer to the desired condition, whereas for loops run a defined number of times and while loops run until the condition is no longer met.

In other words, recursion is declarative because you specify the desired state, whereas for/while loops are iterative because you provide the number of repeats.

Given two numbers the task is to find the product of the given two numbers using recursion in Python.

Examples:

Example1:

Input:

given the first number = 27 
given the second number = 19

Output:

The product of the given numbers 27 and 19 is 27 * 19 = 513

Example2:

Input:

given the first number = 23
given the second number = 38

Output:

The product of the given numbers 23 and 38 is 23 * 38 = 874

Program to Find the Product of two Numbers Using Recursion

Below are the ways to Find the Product of two Numbers using the recursive approach in Python:

1)Using Recursion(Static Input)

Approach:

  • Give the two numbers as static input and store them as two variables.
  • To find the product of the two numbers, pass the numbers as arguments to a recursive function.
  • Give the base condition that if the first number is less than the second, execute the method recursively with the numbers swapped.
  • If the second number is not equal to 0, call the method again recursively otherwise, return 0.
  • The function returns the product of the given two numbers.
  • Print the result using the print() function.
  • The exit of the program.

Below is the implementation:

# function who calculates the product of the given two numbers recursively
def productRecursion(firstnumb, secondnumb):
  # Give the base condition that if the first number is less than the second,
  # execute the method recursively with the numbers swapped.
    if(firstnumb < secondnumb):
        return productRecursion(secondnumb, firstnumb)
    # If the second number is not equal to 0, call the
    # method again recursively otherwise, return 0.
    elif(secondnumb != 0):
        return(firstnumb + productRecursion(firstnumb, secondnumb-1))
    else:
        return 0


# Give the two numbers as static input and store them as two variables.
firstnumb = 27
secondnumb = 19
# passing the given two numbers as arguments to the recursive function
# which returns the product of the given two numbers
# printing th product of th two numbers
print("The product of the given numbers", firstnumb, 'and', secondnumb, 'is ' +
      str(firstnumb)+' * '+str(secondnumb)+' = ', productRecursion(firstnumb, secondnumb))

Output:

The product of the given numbers 27 and 19 is 27 * 19 =  513

2)Using Recursion(User Input separated by spaces)

Approach:

  • Give the two numbers as user input using the map() and split() function and store them in two separate variables.
  • To find the product of the two numbers, pass the numbers as arguments to a recursive function.
  • Give the base condition that if the first number is less than the second, execute the method recursively with the numbers swapped.
  • If the second number is not equal to 0, call the method again recursively otherwise, return 0.
  • The function returns the product of the given two numbers.
  • Print the result using the print() function.
  • The exit of the program.

Below is the implementation:

# function who calculates the product of the given two numbers recursively
def productRecursion(firstnumb, secondnumb):
  # Give the base condition that if the first number is less than the second,
  # execute the method recursively with the numbers swapped.
    if(firstnumb < secondnumb):
        return productRecursion(secondnumb, firstnumb)
    # If the second number is not equal to 0, call the
    # method again recursively otherwise, return 0.
    elif(secondnumb != 0):
        return(firstnumb + productRecursion(firstnumb, secondnumb-1))
    else:
        return 0


# Give the two numbers as user input using the map() and split() function
# and store them in two separate variables.
firstnumb, secondnumb = map(int, input(
    'Enter some random numbers separated by spaces = ').split())
# passing the given two numbers as arguments to the recursive function
# which returns the product of the given two numbers
# printing th product of th two numbers
print("The product of the given numbers", firstnumb, 'and', secondnumb, 'is ' +
      str(firstnumb)+' * '+str(secondnumb)+' = ', productRecursion(firstnumb, secondnumb))

Output:

Enter some random numbers separated by spaces = 23 38
The product of the given numbers 23 and 38 is 23 * 38 = 874

Explanation:

  • The user must provide two integers.
  • To find the product of two numbers, the two numbers are sent as parameters to a recursive function.
  • The basic condition is that if the first number is less than the second, the function is called recursively with the inputs swapped.
  • If the second number is greater than zero, the function is called recursively; otherwise, 0 is returned.
  • The result of the two numbers is printed.

Related Programs: