468,252 Members | 1,468 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,252 developers. It's quick & easy.

Greatest common divisor

19
Hi I'm trying to write a program to find the greatest common divisor of 2 numbers input by the user. This is what I got so far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3.  
  4. int gcd(int a, int b);
  5.  
  6. int main(void)
  7. {
  8.     int a, b,r;
  9.  
  10.     printf("Please input two positive integers");
  11.     scanf("%d%d", &a, &b);
  12.     gcd(a, b);
  13.     if (gcd(b, r))
  14.         printf("%d", gcd);
  15. }
  16.  
  17. int gcd(int a, int b)
  18. {
  19.     int r;
  20.  
  21.     if ((r = a % b) == 0)
  22.         return b;
  23.     else
  24.         return gcd(b, r);
  25. }
  26.  
  27.  
but being the newb that I am. I can't figure out a way to take the data from my gcd function and print it on screen. Any help on ANYTHING I am doing wrong is much appreciated.
Sep 16 '08 #1
4 3218
jmf777
19
never mind figured it out.
Sep 16 '08 #2
donbock
2,422 Expert 2GB
Notice that your gcd function assumes that 'a' is always bigger than 'b'. You might want to add some code to your main function to insure that the first call to gcd() honors that assumption. It would also be a good idea to add comments to gcd stating this assumption.
Cheers,
donbock
Sep 17 '08 #3
hello
i am a cse student (2nd year) in nit srinagar doing B.Tech.
u can try the follwing code.

<full code solution snipped>
Sep 18 '08 #4
Banfa
9,033 Expert Mod 8TB
Hi shakeel225,

posting full code solutions in not allowed on this forum particularly to fairly simple problems that may well be course work or a class assignment.

Please read our posting guidelines where all this is explained.

Banfa
Administrator.
Sep 18 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Mike Nolan | last post: by
21 posts views Thread by Frederick Gotham | last post: by
15 posts views Thread by judge82 | last post: by
35 posts views Thread by aarklon | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.