# Euclid method for solving GCD problem

This is a discussion on Euclid method for solving GCD problem within the C Programming forums, part of the General Programming Boards category; hockeybro12, you're killing me! You really need to read what we post carefully. Take a look again at the outline ...

1. hockeybro12, you're killing me! You really need to read what we post carefully. Take a look again at the outline of the loop I provided in post #12, and take a look at the while loop you posted. What is different between the two?

2. Can anyone help me as it is still not working.

Code:
```#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

main()
{
int num1, num2, rem, x, y;

printf ("This program calculates the GCD of two numbers.\n");
printf ("Enter the first number: ");
num1 = GetInteger();
printf ("Enter the second number: ");
num2 = GetInteger();
x = num1;
y = num2;
rem=(num1%num2);
while (rem != 0)
{

rem=num2;
num2=num1%rem;
num1=rem;
}
printf("The GCD of %d and %d is %d",x,y,num1);
getchar();
}
```

3. Originally Posted by hockeybro12
Can anyone help me as it is still not working.
Of course it's still not working, because YOU DIDN'T CHANGE ANYTHING!!! It's exactly the same code as post #13. And no, we're not going to just give you the answer. Look at the while loop I provided in post #12, and the one you wrote. What is different?

4. Originally Posted by anduril462
Of course it's still not working, because YOU DIDN'T CHANGE ANYTHING!!! It's exactly the same code as post #13. And no, we're not going to just give you the answer. Look at the while loop I provided in post #12, and the one you wrote. What is different?
I did use your solution. The code posted in #17 and #13 is the improved version of the code according to what you provided in 12. I have commented out the other part of what I had before as you can see.

Code:
```
#include <stdio.h>
#include "genlib.h"
#include "simpio.h"

main()
{
int num1, num2, rem, x, y;

printf ("This program calculates the GCD of two numbers.\n");
printf ("Enter the first number: ");
num1 = GetInteger();
printf ("Enter the second number: ");
num2 = GetInteger();
x = num1;
y = num2;
rem=(num1%num2);
while (rem != 0)
{
/*rem = num1 % num2;
if (rem == 0)
{
printf ("The GCD of %d and %d is %d", x, y, num2);
}
else
{
num1 = num2;
num2 = rem;
}
*/
rem=num2;
num2=num1%rem;
num1=rem;
}
printf("The GCD of %d and %d is %d",x,y,num1);
getchar();
}```

5. There is a difference, between the below, and your actual code logic.

Here, a=num1, b=num2, and t=temp (in the below pseudo code), but you are using t as the rem (remainder).

Note that the while loop is b!= 0, not rem != 0, which is what you have.
Code:
```function gcd(a, b)
while b ≠ 0
t := b
b := a mod t
a := t
return a```
It would help if you changed your variables to these same names, for just a minute. That would eliminate some confusion.

6. Originally Posted by hockeybro12
I did use your solution. The code posted in #17 and #13 is the improved version of the code according to what you provided in 12. I have commented out the other part of what I had before as you can see.
&@*\$!@&*\$&%#%*^(@#^)#\$&*@#@&\$ No you didn't!!!

Me (post #12): Here is a loop to use
You (post #13): Here's my new code, it doesn't work.
Me (post #17): You got the loop wrong. Reread my post #12.
You (post #18): I didn't change my broken code, but it's still broken.
Me (post #19): Of course, if you don't fix broken code, it remains broken. Fix your while loop, look at my post #12.
You (post #20): I used your solution. Look (same broken code from post #13 and #17 that uses a different while loop than what I provided).
Me (in this post): &@*\$!@&*\$&%#%*^(@#^)#\$&*@#@&\$ No you didn't!!!

I'm being a bit of an a-hole here because it seems you're just not paying attention. All you had to do was copy-paste the while (num2 != 0) line from my post #12, and you would have this thing solved. Or you could have looked at the line, and typed it character by character. You had similar lack-of-careful-reading-or-thinking-about-what-you-are-doing issues when you were changing == to = with no regard to which lines were already correct and which were problematic, despite me posting warnings with line numbers. If you can't be bothered to read and think carefully about what you're doing, then programming is not for you (actually, there is very little for you if you can't do those two things). Also, if you fail to follow our suggestions or pay attention to us when you asked us for help, we rapidly lose interest in helping you in the future.

7. Originally Posted by anduril462
&@*\$!@&*\$&%#%*^(@#^)#\$&*@#@&\$ No you didn't!!!

Me (post #12): Here is a loop to use
You (post #13): Here's my new code, it doesn't work.
Me (post #17): You got the loop wrong. Reread my post #12.
You (post #18): I didn't change my broken code, but it's still broken.
Me (post #19): Of course, if you don't fix broken code, it remains broken. Fix your while loop, look at my post #12.
You (post #20): I used your solution. Look (same broken code from post #13 and #17 that uses a different while loop than what I provided).
Me (in this post): &@*\$!@&*\$&%#%*^(@#^)#\$&*@#@&\$ No you didn't!!!

I'm being a bit of an a-hole here because it seems you're just not paying attention. All you had to do was copy-paste the while (num2 != 0) line from my post #12, and you would have this thing solved. Or you could have looked at the line, and typed it character by character. You had similar lack-of-careful-reading-or-thinking-about-what-you-are-doing issues when you were changing == to = with no regard to which lines were already correct and which were problematic, despite me posting warnings with line numbers. If you can't be bothered to read and think carefully about what you're doing, then programming is not for you (actually, there is very little for you if you can't do those two things). Also, if you fail to follow our suggestions or pay attention to us when you asked us for help, we rapidly lose interest in helping you in the future.
I'm really sorry. I forgot to change the while loop to num2 instead of rem. I have changed it now and it is working. I will be more careful later.

Page 2 of 2 First 12