Calculating GCD

This is a discussion on Calculating GCD within the C Programming forums, part of the General Programming Boards category; Hello, I have made a programm which calculated the GCD of two numbers. Code: #include <stdio.h> int main ( int ...

  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,682
    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,185
    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 02: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...

Page 1 of 2 12 LastLast
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, 05:30 PM
  2. calculating the mean
    By bartleby84 in forum C Programming
    Replies: 9
    Last Post: 08-27-2007, 12:47 PM
  3. calculating cos
    By lilhawk2892 in forum C++ Programming
    Replies: 5
    Last Post: 09-28-2005, 03:39 PM
  4. Help with calculating
    By Moffia in forum Windows Programming
    Replies: 3
    Last Post: 08-05-2005, 02:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21