Multiplication of two integers in C++ using recursion

In this tutorial, we will learn how to multiply two integers in C++ using recursion. Recursion is used to call the function by itself.  Multiplication of two numbers can be performed in different ways, and multiplication of two numbers by use of recursion is one of the ways. Doing the multiplication by recursion is discussed in the below problem.

Program to find the multiplication of two numbers

In this problem, we use the header and then we declare two integers and assign them a value, so that we can find the values by multiplication. In this code, we assume a base case while applying the concept of recursive functions. we create a function named  (multiply) and pass the values in it and then we assume the base case as if the element is 0 then return 0. In that way, we calculate the product of two numbers.

#include <iostream>
using namespace std;
int multiply(int m,int n){
    if(n==0)
    return 0;
    return multiply(m,n-1)+m;
}
int main(){
    int m=13,n=18;
    cout<<multiply(m,n);
    
}

In the above code, we have assigned the values of two integers as 13 and 18.  And by the use of the above code, we can find the values after the multiplication and the answer is 234.

The output of the code is:

234

Also, read: Sum of elements in an array using recursion in C++

Leave a Reply

Your email address will not be published. Required fields are marked *