- Related Questions & Answers
- How to find the product of 2 numbers using recursion in C#?
- How to find the product of two binary numbers using C#?
- Java program to calculate the product of two numbers
- C++ Program to Find Fibonacci Numbers using Recursion
- C++ program to Find Sum of Natural Numbers using Recursion
- Python Program to Find the Length of a List Using Recursion
- Program to find the largest product of two distinct elements in Python
- How to Find Sum of Natural Numbers Using Recursion in Python?
- Python Program to Find the Length of the Linked List using Recursion
- Program to find sign of the product of an array using Python
- Python Program to Find the Fibonacci Series without Using Recursion
- Program to find out the dot product of two sparse vectors in Python
- Python Program to Find the Length of the Linked List without using Recursion
- Find HCF of two numbers without using recursion or Euclidean algorithm in C++
- Python Program to Find the Total Sum of a Nested List Using Recursion

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

When it is required to find the product of two numbers using recursion technique, a simple if condition and recursion is used.

The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.

Below is a demonstration for the same −

def compute_product(val_1,val_2): if(val_1<val_2): return compute_product(val_2,val_1) elif(val_2!=0): return(val_1+compute_product(val_1,val_2-1)) else: return 0 val_1 = int(input("Enter the first number... ")) val_2 = int(input("Enter the second number... ")) print("The computed product is: ") print(compute_product(val_1,val_2))

Enter the first number... 112 Enter the second number... 3 The computed product is: 336

- A method named ‘compute_product’ is defined, that takes two numeric values as paramters.
- If the first value is less than second value, then the function is called again by swapping these parameters.
- If the second value is 0, the function is called by passing first value, and subtracting ‘1’ from second value, and adding the first value to the result of the function.
- Otherwise the function returns 0.
- Outside the function, two numbers value are entered by the user.
- The method is called by passing these two values.
- The output is displayed on the console.

Advertisements