problem on c programming

This is a discussion on problem on c programming within the C Programming forums, part of the General Programming Boards category; I have a problem determining the greatest common divisor of any two numbers. The question goes as follows; The greatest ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    problem on c programming

    I have a problem determining the greatest common divisor of any two numbers. The question goes as follows;

    The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers. Write a function gcd the "gcd" that returns the greatest common divisor of two integers.
    This question is taken from INTRODUCING C++ AND JAVA /HOW TO PROGRAM C ( THIRD EDITION ) AUTHOR: DEITEL & DEITEL
    PLEASE I WILL BE GRATEFUL IF THIS PROBLEM IS SOLVED AND REPLIED TO ME QUICKLY.
    THANKS
    MIKE

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    int GCD(int a,int b)
    {
    int c;

    a=a>0?a:-a;
    b=b>0?b:-b;
    if(a<b){
    c=a;
    a=b;
    b=c;
    }
    if(!b)return(a);

    while(c=a%b){
    a=b;
    b=c;
    }
    return(b);
    }

    BTW: i haven't tested it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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