# Calculating GCD

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-26-2011
roelof
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
• 05-26-2011
ssharish2005
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
• 05-26-2011
tabstop
Also, why do you bother with tmp?
• 05-26-2011
roelof
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
• 05-26-2011
CommonTater
Quote:

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

Roelof

:D I just hate it when he does that! :D
• 05-26-2011
roelof
Commontator :

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

Roelof
• 05-26-2011
CommonTater
Quote:

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 )
• 05-26-2011
roelof
Oke,

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

Roelof
• 05-26-2011
CommonTater
Quote:

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?
• 05-27-2011
roelof
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
• 05-27-2011
CommonTater
Cool... sounds like fun.
• 05-27-2011
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
• 05-27-2011
CommonTater
Quote:

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.
• 05-27-2011
roelof
Commontator:

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

Roelof
• 05-27-2011
CommonTater
Thanks...
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last