Loop statement issues

I am not by any means looking for someone to do a problem for me but I am really struggling conceptually, I need a push in the right direction. I am writing a program to find the greatest common divisor of two integers. Essentially I have to take an integer say 735, divide by 252 and get a remainder of 231, then take that remainder and put it into 252 yielding the remainder 21, and so on until the final remainder is 0. Once it is zero the loop would close. I've tried with a for statement but can't figure out what the initializing or update expressions should be. Any help would be greatly appreciated.
Last edited on
You can do this with a for loop, but it's called a for loop for a reason; a for loop is best used when you want to loop for this many times. Do you know how many times you want to loop? No.

You want to loop while the remainer is not zero.

1
2
3
4
5
6
7
while (remainder != 0)
{
  remainder = numerator % denominator;
  numerator = denominator;
  denominator = remainder;
}
// numerator is now the gcd 
Last edited on
Thanks, I will give that a try.
Topic archived. No new replies allowed.