# 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
• 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.