

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Find the Product of two Numbers Using Recursion
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.
Example
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))
Output
Enter the first number... 112 Enter the second number... 3 The computed product is: 336
Explanation
- 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.
- Related Questions & Answers
- Java Program to Find the Product of Two Numbers Using Recursion
- 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 Find the Sum of Natural Numbers using Recursion
- Python program to find product of rational numbers using reduce function
- C++ Program to Find Fibonacci Numbers using Recursion
- C++ program to Find Sum of Natural Numbers using Recursion
- Java Program to Find Sum of N Numbers Using Recursion
- Java program to calculate the product of two numbers
- Python program to find Cartesian product of two lists
- Python Program to Find the Fibonacci Series Using Recursion
- How to Find Sum of Natural Numbers Using Recursion in Python?
- Python Program to Find the Length of a List Using Recursion
- Python Program to Find the Length of the Linked List using Recursion
- Program to find the largest product of two distinct elements in Python
Advertisements