Thread: code for getting the GCD (greatest common divisor)

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    20

    code for getting the GCD (greatest common divisor)

    can someone help there seem to be a problem with my code

    Code:
    #include<stdio.h>#include<conio.h>
    #include<math.h>
    main()
    {
            int gcd(int a,int b)
            {
                int c;
                while(1)
                {
                       c = a%b;
                       if(c==0)
                      return b;
                       a = b;
                       b = c;
                    }
                }
                getch();
            }

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Yes, there's many problems:
    - Don't use implicit main > SourceForge.net: Implicit main - cpwiki
    - No idea what you're trying to do nesting functions, but it doesn't work
    - Lose the getch() and the outdated conio.h header
    - Learn how to use while loops correctly, and realize that infinite loops with forced termination are bad code
    - Your algorithm seems pretty screwy anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The greatest common divisor (GCD) help..
    By damha in forum C Programming
    Replies: 4
    Last Post: 04-09-2011, 05:18 AM
  2. Greatest Common Divisor.....
    By muran_pling in forum C++ Programming
    Replies: 10
    Last Post: 12-18-2006, 05:02 AM
  3. Greatest Common Divisor problem
    By fenixataris182 in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 07:55 PM
  4. Greatest common divisor
    By wiz23 in forum C++ Programming
    Replies: 5
    Last Post: 04-13-2005, 04:50 PM
  5. Greatest common divisor with int and double
    By wiz23 in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2005, 04:38 PM