1. ## Calculating GCD

Hello,

I have made a programm which calculated the GCD of two numbers.
Code:
```
#include        <stdio.h>

int
main ( int argc, char *argv[] )
{
int getal1, getal2, tmp, uitkomst, rest ;

tmp = 0 ;
printf ("Enter the two numbers where the GCD must be calculated  \n")
printf ("Please first the little number and then the greatest number. ");
scanf ("%d %d", &getal1, &getal2) ;
uitkomst = getal2 / getal1 ;
rest  = getal2 - uitkomst * getal1;
while (rest > 1)
{
tmp = getal1 ;
getal2 = getal1 ;
getal1 = rest ;
uitkomst = getal2/getal1 ;
rest = getal2 - uitkomst * getal1;
}
if (rest == 0)
printf("The GCD is %d",getal1 );
else
printf("There is no GCD");
return 0 ;```
For me it works.
But I wonder if you experts have anything to say about the programm.

Roelof

2. Code:
```uitkomst = getal2 / getal1 ;
rest  = getal2 - uitkomst * getal1;```
Not quite sure why you do that. It as simple as this
Code:
```uitkomst = getal2 % getal1 ;

while( uitkomst  != 0)
{
...
}```
An also you can do this recursively

Code:
```int gcd( int a, int b )
{
if( b == 0)
{
printf("%d\n", a);
return a;
}
else
{
printf("%d %d\n", a,b);
return gcd( b, a%b );
}```
ssharish

3. Also, why do you bother with tmp?

4. oke,

Let's say we use the numbers 210 and 57
So uitkomst will be 210 / 57 = 3
rest will be 210 - 3*57 = 39
tmp = 57
getal2 = 57
getal1= 39
uitkomst = 57 / 39 = 1
rest = 57 - 39 = 18
and so on.

tabstop . yore right. I don't need tmp.

Roelof

5. Originally Posted by roelof
oke,
tabstop . yore right. I don't need tmp.

Roelof
I just hate it when he does that!

6. Commontator :

I don't get it what you mean with your message.

Roelof

7. Originally Posted by roelof
Commontator :

I don't get it what you mean with your message.

Roelof
Hmmm... ok ... it's a humourous way to convey that I am slightly envious of his skill.

( but only slightly )

8. Oke,

and any remarks on my program where I can learn things.

Roelof

9. Originally Posted by roelof
Oke,

and any remarks on my program where I can learn things.

Roelof
Looks good to me. Probably pretty close to what I would write (but I'm no math head...). It looks like you did some pruning and minimizing which is always a good thing.

I think you're doing just fine my friend.

So, what's next?

10. next 2 assigments are :

1) Write a program that's ask a user to enter a fraction, then reduces the fraction to the lowest term.

That's a easy one. I only have to change some things in the GCD programm

2) Change a earlier programm so the user can calculate things till the user enters 0

That are assigments 3 and 4 of the 12 of the loop chapter

Roelof

11. Cool... sounds like fun.

12. Hello,

Yep, a lot of fun.
I can send you a pdf copy of the book im following.

Next chapter is about the data types with a lot of execting exercices.

Roelof

13. Originally Posted by roelof
Hello,

Yep, a lot of fun.
I can send you a pdf copy of the book im following.

Next chapter is about the data types with a lot of execting exercices.

Roelof
If you could post a link to it... I'd like to add it to my collection.

14. Commontator:

Here 1 link I found the book : C Programming: A Modern Approach, 2nd Edition (Repost)

Roelof

15. Thanks...