|
-
Feb 21st, 2002, 07:31 PM
#1
Thread Starter
Junior Member
Russian Peasant Calculation Problem
Hi there, I'm trying to write a program to solve the multiplication of two numbers using the Russian Peasant method. If you don't know what this method is you will find out @
http://mathforum.org/dr.math/faq/faq.peasant.html
Unfortnately when I iterate round the loop the test condition needs to be changed to give a correct answer. Is there some way of making the test condition in the while loop dynamic so that the program will display the correct result ?
As far as I know this problem should be solvable by using a do - while loop & an if statement.
Any help/advice would be greatly appreciated.
I know that this code will just create an infinte loop because there is just one value of 'b' and its just running the same test over and over ..... etc
Cheers
CaptainChainsaw 
Here is what I've got so far:
#include <iostream>
using namespace std;
void main() {
int a, b, sum;
cout << "Enter a number for a: ";
cin >> a;
cout << "Enter a number for b: ";
cin >> b;
while(b%2>=1) {
sum+=a*2;
if(b%2 == 0){
sum-=a*2;
}
}
cout << sum << " is the answer!!!" ;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|