>>If the larger number is odd,
what happens if the larger number is even ?
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Write it out by hand. It might start like this:
enter two numbers.
calculate the product of the two numbers.
determine which number is larger, call it A, and call the other number B.
use an accumulator, initialzed to zero
as long as A is above zero
if A is odd
add B to accumulator
divide A by 2 using integer math (means all remainders are dropped)
//etc
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
and if A is initially even, don't do anything.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
if A is even at anypoint, whether initially or not, then don't add B to accumulator, but you still need to divide A by 2 using integer math and double B. So if A = 44 and B = 11;
A B Total Accumulated so far
44 11 0
22 22 0
11 44 44
5 88 132
2 176 132
1 352 484
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Problem is that x is not initialized to zero (I take that you simply want to swap the values).
You might have it a bit simpler like ...
if (numB < numA)
{
// use x only inside the if-block
int x = numA;
numA = numB;
numB = x;
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395