Russian Peasant calculator URGENT

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
you've already got a thread.
What do you mean
Question 2

// ConsoleApplication15.cpp : Defines the entry point for the console application.
// My Program for Question 2 of my assignment
// 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:

// Russian Peasant Calculator.cpp :


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

using namespace std;

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

{

HWND hWnd = GetConsoleWindow(); // GetConsoleWindow function

ShowWindow(hWnd,SW_SHOWMAXIMIZED); // ShowWindow function

// variables

char answer;
do

{

int a, b, result;

a = 0;
b = 0;
result = 0;

// Prompt for first input
cout << "\n\n\tPlease enter your first number. ";
cin >> a;
// Prompt for second input
cout << "\n\n\tPlease enter your second number ";
cin >> b;

cout << "\n\n\t" << a; cout << "\t" <<setw(8) << b;

while (a != 1)

{

if(a%2 >= 1)

{

result += b;

}

a = a/2;

b = b*2;

cout << "\n\n\t" << a;

cout <<"\t" <<setw(8) << result+b;

}

result += b;

cout << "\n\n\tYour result is. " << result;

do

{

cout << "\n\n\tWould you like to run the program again? [Y/N] "; // Do you want to run the program again Y Or N

cin >> answer;

answer = toupper(answer); // Uppercase Answer




if (answer !='Y' && answer != 'N')

cout << "\n\n\tInvalid input. Please re-enter.";

else

system("cls");

} while (answer != 'Y' && answer != 'N');




} while (answer != 'N');

cout << "\n\n\tPress any key to EXIT the Program.";

_getch();

return 0;

}
Topic archived. No new replies allowed.