Home >>Cpp Programs >C++ Program to multiply two numbers without using multiplication operator
In this example, we will see a C++ program through which we can multiply two numbers without using the multiplication operator.
In this program, we will use the Russian peasant algorithm where we will assume that the two given numbers are m and n. Initialize mul with 0 and repeat the following steps while n is greater than zero :
#include <iostream>
using namespace std;
int Multiply(int m, int n)
{
int mul=0;
while (n > 0)
{
// if n is odd
if (n & 1) mul = mul + m;
// Double 'm' and halve 'n'
m = m << 1;
n = n >> 1;
}
return mul;
}
int main() {
int ans;
ans=Multiply(5,15);
cout<<"Multiplication of 5 and 15 = "<<ans<<endl;
ans=Multiply(9,43);
cout<<"Multiplication of 9 and 43 = "<<ans<<endl;
return 0;
}