# Thread: question on GCD and for loops

1. ## question on GCD and for loops

ok im doing this other program on for loops for greatest common divisor, so the problem is i cant seem to display the common divisor of both numbers, so i seem to be having problems with the formula inside the for loop, so it doesn't work is there anything im missing

Code:
```#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
int g,n,m,i;

printf("Give me two numbers separated by space (p>q): ");
scanf("%d %d",&n,&m);

for(i=0;i<=n%m;i++){

g=n%m;
g=m%n;
}

printf("The GCD of %d and %d is %d ",n,m,g);
getchar();
getchar();

}```
also another question how can i do to insert two numbers in one enter (insert two numbers just by spacing them?)

2. I think you'd learn a lot by trying to explain to us in your own words what this does:
Code:
```             for(i=0;i<=n&#37;m;i++){

g=n%m;
g=m%n;
}```
It does not at all do what you want.

If you need a reference source for this algorithm, here's one:
http://en.wikipedia.org/wiki/Euclidean_algorithm
btw this algorithm is extremely common in computer science teaching. If you google it, you can expect to find over a million hits.

3. the modulus equation in that part give me

g=150/62=2.419 but the modulus sign rounds it off to the next lowest whole number

ohhhh i see the modulus thing in the for statement inside the parethesis, i was trying out something to see if it worked but it didn't and i forgot to change it, anyways the Euclidean Algorithm helps, but it doesn't use what i want to use a for loop instead of a while loop

i really don't know im really lost on what to do next

4. i still haven't solved this problem, somehow always all the easy stuff throws me off

anyways, if anyone can help me with this it'll be great
thanks

5. What does MODULUS (%) do? It seems like you are confused about that.

Also the code-segment that iMalc posted is quite likely NOT what you want [at least not exactly like it is now].

--
Mats

6. oh modulus &#37; find the value of the remainder of the equation or division

7. Originally Posted by dals2002
ohhhh i see the modulus thing in the for statement inside the parethesis, i was trying out something to see if it worked but it didn't and i forgot to change it, anyways the Euclidean Algorithm helps, but it doesn't use what i want to use a for loop instead of a while loop
Why would you want to use a for-loop? A for-loop is the most logical choice when there are a fixed number of iterations, as well as a few other cases. In this case it is clearer with a while loop. It is easy to write any kind of loop using any of the loop constructs anyway, so you could always learn to convert between them.

Anyway, ignoring the for-loop for now, are you at least able to now explain what the folowing does?:
Code:
```               g=n%m;
g=m%n;```
Do you understand what is wrong with that?

8. Originally Posted by iMalc
Why would you want to use a for-loop? A for-loop is the most logical choice when there are a fixed number of iterations, as well as a few other cases. In this case it is clearer with a while loop. It is easy to write any kind of loop using any of the loop constructs anyway, so you could always learn to convert between them.

Anyway, ignoring the for-loop for now, are you at least able to now explain what the folowing does?:
Code:
```               g=n&#37;m;
g=m%n;```
Do you understand what is wrong with that?
yea that gives me the remainder of the division, it gets higher the higher the numbers, but it doesn't give me what i want which is the opposite of the remainder. i was asked in the book to use a for loop, since i haven't got to read the chapter on while loops yet

9. You might want to search for the words "Euclidean algorithm", and look at the pseudo-codes
presenting the algorithm, as you have it clearly a little messed up.

10. Originally Posted by dals2002
yea that gives me the remainder of the division, it gets higher the higher the numbers, but it doesn't give me what i want which is the opposite of the remainder. i was asked in the book to use a for loop, since i haven't got to read the chapter on while loops yet
Okay, nope you don't understand at all what is wrong with those two lines.
Forget loops and modulus, you need to go back to understanding variable assignment.

What does this code snippet do?:
Code:
```int x;
x = 1;
x = 2;```
Can you simplify it?