C++ program to implement Russian peasant algorithm
#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(2,3);
cout<<"Multiplication of 2 and 3 = "<<ans<<endl;
ans=Multiply(10,10);
cout<<"Multiplication of 10 and 10 = "<<ans<<endl;
return 0;
}
Output
Multiplication of 2 and 3 = 6
Multiplication of 10 and 10 = 100
C++ program to implement Russian peasant algorithm
Output