
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Find the Product of Two Numbers Using Recursion
Recursion is a technique where we call a function from the same function itself. There must be some base or terminating condition to end the recursive call. Recursive procedures are very much helpful to perform complex iterative solution with fewer codes and easier solution approach through sub-operation finding.
In this article we shall discuss a recursive approach to perform product (multiplication) between two numbers in C++. Initially we will understand the basic principle, the syntax for recursive function call, the algorithm and the source code.
Multiplication using Recursion
In high-level languages, there is the multiplication operators available to directly perform multiplication. However we know that the multiplication is actually a repetitive addition. So the result of A * B is repetitive addition of A, B number of times, or we can say repetitive addition of B, A number of times. Whenever there is a repetition, we can do this using recursion. Let us see the recursive function definition syntax first.
Syntax
<return type> function_name ( parameter list ) { if ( base condition ) { terminate recursive call } recursive function call: function_name ( updated parameter list ) }
Algorithm
Let us see the algorithm to perform multiplication using recursion.
- define a function multiply() which takes two numbers A and B
- if A < B, then
- return multiply( B, A )
- otherwise when B is not 0, then
- return A + multiply( A, B - 1 )
- otherwise
- return 0
- end if
- if A < B, then
- end of function definition
- Read two inputs A and B
- res = multiply( A, B )
- display res
Example
#include <iostream> #include <sstream> using namespace std; int multiply( int A, int B) { if( A < B ) { return multiply( B, A ); } else if( B != 0 ) { return A + multiply( A, B - 1 ); } else { return 0; } } int main() { cout << "Multiplication of 5, 7 is: " << multiply( 5, 7 ) << endl; cout << "Multiplication of 8, 0 is: " << multiply( 8, 0 ) << endl; cout << "Multiplication of 25, 3 is: " << multiply( 25, 3 ) << endl; cout << "Multiplication of 9, 1 is: " << multiply( 9, 1 ) << endl; }
Output
Multiplication of 5, 7 is: 35 Multiplication of 8, 0 is: 0 Multiplication of 25, 3 is: 75 Multiplication of 9, 1 is: 9
See, in this program, the function is taking parameters A and B both are two integer numbers. Now after each step it reduces the second parameter B by 1 and adding A with A itself. Like this the function is performing multiplication procedure.
Conclusion
Recursion is a process to call the same function from the function itself. While calling a function recursively, we update or change the parameter set a little so that the same effect is not coming again and again, then it divides the problem into smaller subproblems and solve the problem by solving these smaller problems in a bottom-up manner. Almost anything which can be implemented using loops, can also be implemented using recursion. In this article we have seen simple process to multiply two integers using recursion. The integers are added multiple times to get the final multiplication result.
- Related Articles
- Python Program to Find the Product of two Numbers Using Recursion
- Java Program to Find the Product of Two Numbers Using Recursion
- Haskell Program to Find the Product of Two Numbers Using Recursion
- Golang Program to Find the Product of Two Numbers Using Recursion
- How to find the product of 2 numbers using recursion in C#?
- Haskell Program to find the LCM of two given numbers using recursion
- Haskell Program to find the GCD of two given numbers using recursion
- Swift program to find the GCD of two given numbers using recursion
- C++ Program to Find Fibonacci Numbers using Recursion
- C++ program to Find Sum of Natural Numbers using Recursion
- How to find the product of two binary numbers using C#?
- Java Program to Find the Sum of Natural Numbers using Recursion
- Haskell Program to Find the Sum of Natural Numbers using Recursion
- Golang Program to Find the Sum of Natural Numbers using Recursion
- Golang Program to Find the Sum of N Numbers using Recursion
