Q:

C++ program to multiply two numbers without using multiplication operator

belongs to collection: C++ programs on various topics

0

C++ program to multiply two numbers without using multiplication operator

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ programs on various topics

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ program to find LCM of two numbers... >>
<< Sorting a structure in C++...