C++ Please Help

2. Write a program to act as a Russian Peasant Multiplication Calculator. This program should allow the user to carry out multiplication by just simply multiplying or dividing by 2 (and addition). It works as follows.

Start with two values, A and B. The goal is to multiply A by B. The first number, A, is successively divided by 2 while the second number, B, is multiplied by 2. If the number in the first column is odd, then the corresponding number in the second column is added to the sum. The final sum is the product, eg:

A B Product
37 41 0 + 41 = 41
18 82
9 164 41 + 164 = 205
4 328
2 656
1 1312 205 + 1312 = 1517

The answer from the calculation is 1517 which is the answer obtained by multiplying 37 by 41.

Allow the user to run the program more than once if so desired.

prompt the user to try again Y/N at the end, This program works fine below.


#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <stdlib.h>
#include <string>
#include <math.h>
#include <iomanip>


using namespace std;


int _tmain(int argc, _TCHAR* argv[])

{
int a,b, sum = 0; // Variables


//Asking the user for input
cout <<"\n\n\t Please Enter A Number For a "; // Number for a
cin >> a; // input

cout <<"\n\n\t Please Enter A Number For b "; // Number for b
cin >> b; //input

while (b!=1)
{
if(b%2>=1)
{
sum+=a;
}
b=b/2;
a=a*2;
}
sum+=a;
cout << "\n\n\t The answer is " << sum << "\n\n";



_getch();
return 0;
}
Last edited on
what is this?
Question on an assignment I have
This should go under the Beginners section, I think you can edit the topic to move it.
Also put your code between [ code][ /code] tags.
Topic archived. No new replies allowed.