Thread: The greatest common divisor (GCD) help..

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    The greatest common divisor (GCD) help..

    I made this program (using C not C++):

    Code:
    #include<stdio.h>
    int num1, num2;
    int main() {
    	printf("Type in two numbers: ");
    	scanf("%d %d", &num1, &num2);
    	while (num1!=num2){
    		if (num1>num2){
    			num1=num1-num2;
    		} else {
    		num2=num2-num1;
    		}
     }
    	printf("The GCD is: \n%d");
    }
    but it's not working correctly. I'm suppose to use 2 integers and like 21 and 15 and then I have to get the GCD, and the GCD should be 3 out of 21 and 15 but in my programme it display 2293624 for any 2 numbers.
    Can someone please explain what did I do wrong and how can I fix it
    Thanks
    Last edited by Salem; 04-08-2011 at 11:51 PM. Reason: added [code][/code] tags - learn to use them yourself

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    50

    Thumbs up

    print the result.
    u havent printed anything.

    2 questions i want to ask to you damha:
    1. the program seems to be ok. but whats the mathetical reason behind it. i did the same program but it was a longer one(not the source code, but the process).
    2.what compiler are you using?
    Last edited by Abhas; 04-08-2011 at 11:31 PM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I haven't bothered to check the algorithm but your printf("The GCD is: \n%d") is missing a parameter.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Quote Originally Posted by Abhas View Post
    print the result.
    u havent printed anything.

    2 questions i want to ask to you damha:
    1. the program seems to be ok. but whats the mathetical reason behind it. i did the same program but it was a longer one(not the source code, but the process).
    2.what compiler are you using?
    Well I have to get the the greatest common divisor from the 21 and 15, and I'm using C.
    Hope that answered your questions

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Thanks guys it was a silly problem XD, I didn't add the integer in printf like nonoob says.
    It supposed to be printf("The GCD is: %d/n" num1) lol that was easy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Greatest Common Divider
    By Bennie98 in forum C Programming
    Replies: 10
    Last Post: 11-15-2010, 09:33 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
    By wiz23 in forum C++ Programming
    Replies: 5
    Last Post: 04-13-2005, 04:50 PM
  4. Greatest common divisor with int and double
    By wiz23 in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2005, 04:38 PM
  5. Greatest Common Factor problem
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 03:29 PM