Greatest Common Divisor problem

This is a discussion on Greatest Common Divisor problem within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble with creating a program that calculates the greatest common divisor between two numbers. I would greatly appreciate ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    15

    Greatest Common Divisor problem

    I'm having trouble with creating a program that calculates the greatest common divisor between two numbers. I would greatly appreciate some help.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    double gcd ( int x, int y );
    
    int main()
    {
       int a;
       int b;
    
       // allow the five sets of numbers to be input
       for ( int j = 1; j <= 5; ++j ) {    
          cout << "Enter two integers: ";
          cin >> a >> b;
          cout << "The greatest common divisor of " << a 
               << " and " << b << " is "
               << gcd ( a, b ) << "\n\n";
    
       } // end for
    
       return 0;
    
    } // end main
    
    // function gcd definition
    double gcd( int x, int y )
    {
       int greatest = 1;
    
       for ( int i = 2; i <= ( ( x < y ) ? x : y ); ++i );
    
          if  0 = ( x % i ) + ( y % 1 ); 
             greatest = i;
    
       return greatest;
    
    } // end function gcd

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    You didn't say what the problem was. A compiler error? Bad output?

    I would look at this line first (that's not how you do an if test for equality):
    Code:
    if 0 = ( x % i ) + ( y % 1 );

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that, and the semicolon at the end of for(...) is a problem... and you should really use curly braces there... other than that, It's fine (I didn't really check for logic errors, but that looks okay)

    the only thing I'd suggest is letting people exit your program somehow...

    Code:
    double gcd( int x, int y )
    {
       int greatest = 1;
    
       for ( int i = 2; i <= ( ( x < y ) ? x : y ); ++i )
       {
    
          if ( 0 == ( x % i ) + ( y % 1 ))
             greatest = i;
       }
    
       return greatest;
    
    } // end function gcd
    Last edited by major_small; 07-12-2005 at 08:02 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    nevermind im not paying attention,
    Last edited by ILoveVectors; 07-12-2005 at 06:56 PM.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    that way works too... with a little modification... or you could also do:
    Code:
    if(!((x%i)+(y%1)));
    although I wouldn't.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    This code makes no sense. y % 1 evaluates to zero. Always.

    Fenix, you need to learn your C++ syntax before anything else.

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    must be a typo, sure they ment, 'i' there to

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    15
    I worked on it some more and it still has problems. It won't calculate the greatest common divisor. No matter what it always comes up with the lowest of the two numbers. What needs to be fixed?
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    double gcd ( int x, int y );
    
    int main()
    {
       int a;
       int b;
    
       // allow the five sets of numbers to be input
       for ( int j = 1; j <= 5; ++j ) {    
          cout << "Enter two integers: ";
          cin >> a >> b;
          cout << "The greatest common divisor of " << a 
               << " and " << b << " is "
               << gcd ( a, b ) << "\n\n";
    
       } // end for
    
       return 0;
    
    } // end main
    
    // function gcd definition
    double gcd( int x, int y )
    {
       int greatest = 1;
    
       for ( int i = 2; i <= ( ( x < y ) ? x : y ); ++i )
       {
    
          if ( 0 == ( x % i ) + ( y % i ));
             greatest = i;
       }
    
       return greatest;
    
    } // end function gcd

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,325
    You forgot to remove the semi-colon after the if statement.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Greatest Common Divisor.....
    By muran_pling in forum C++ Programming
    Replies: 10
    Last Post: 12-18-2006, 04:02 AM
  2. Greatest common divisor
    By wiz23 in forum C++ Programming
    Replies: 5
    Last Post: 04-13-2005, 04:50 PM
  3. Greatest common divisor with int and double
    By wiz23 in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2005, 04:38 PM
  4. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 03:33 AM
  5. Greatest Common Factor problem
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 03:29 PM

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