Thread: Greatest common divisor

  1. #1
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48

    Greatest common divisor

    I am working on the greatest common divisor (gcd), previously I have already declare a = 667 and c = 140 and now I am trying to get the (gcd) working. The answer to this is 1. Here is my code:
    Code:
    int gcd(int a, int c);
    {
     int g;
     g = a;
     while (a%g!=0||c%g!=0)
    {
    g--;
    }
    return g;
    }
    Is this the right code for (gcd). If so I find it hard to display on the screen as once compile it then disappear. Is there a way that can make g display itself such as
    Code:
    cout << g << endl;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Is this the right code for (gcd).
    Test and find out.
    You should get an error concerning a certain semi-colon placed inappropriately.

    Offhand the algorithm probably works, but looks inefficient.
    Search the Web for a GCD algorithm by the very late Euclid.

    If so I find it hard to display on the screen as once compile it then disappear.
    Run your program from command line, or do a
    std::cin.sync(); std::cin.get();
    before returning 0 in main().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    FAQ > How do I... (Level 1) > Stop my Windows Console from disappearing everytime I run my program?
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    Search the board for GCD.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Putting aside any syntax-related problems your code has, it should work to calculate the GCD. Of course, if you want to do it efficiently, you will want to use the Euclidean algorithm.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    T-Mac wiz23's Avatar
    Join Date
    Apr 2005
    Location
    Houston
    Posts
    48
    Is this GCD code alright?
    Code:
    int gcd (int x, int y)
    {
     if (x % y == 0)
      return y;
     else
      return gcd (y, x % y);
    }
    But for my one, I am currently using a = 667 and c = 140, so does this mean is like
    Code:
    int gcd (int a, int c)
    {
     if (a % c == 0)
      return c;
     else
      return gcd (c, a % c);
    }
    If so, how can I display the gcd on the screen because the return function will eventually disappear. Is there a way that the
    Code:
    cout <<
    can be used here so I can display it.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    cout<<gcd(667,140)<<endl;
    That should work just fine.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

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, 05:02 AM
  2. Greatest Common Divisor problem
    By fenixataris182 in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 07:55 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 Factor
    By NavyBlue in forum C Programming
    Replies: 5
    Last Post: 06-11-2002, 02:47 PM
  5. Greatest Common Factor problem
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 03:29 PM