Thread: Calculating GCD

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    230

    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. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    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
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Also, why do you bother with tmp?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    230
    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. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    oke,
    tabstop . yore right. I don't need tmp.

    Roelof
    I just hate it when he does that!

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    230
    Commontator :

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

    Roelof

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    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 )
    Last edited by CommonTater; 05-27-2011 at 01:36 AM.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    230
    Oke,

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

    Roelof

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    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. #10
    Registered User
    Join Date
    May 2010
    Posts
    230
    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. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Cool... sounds like fun.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    230
    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. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by roelof View Post
    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. #14
    Registered User
    Join Date
    May 2010
    Posts
    230
    Commontator:

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

    Roelof

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating Pi with GMP
    By blkhockeypro19 in forum C Programming
    Replies: 0
    Last Post: 06-10-2010, 04:30 PM
  2. calculating the mean
    By bartleby84 in forum C Programming
    Replies: 9
    Last Post: 08-27-2007, 11:47 AM
  3. calculating cos
    By lilhawk2892 in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2005, 02:39 PM
  4. Help with calculating
    By Moffia in forum Windows Programming
    Replies: 3
    Last Post: 08-05-2005, 01:21 AM