code_junkie

Beginner C Programmer needs help with pointers ( I think )

Nov 13th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. int greatestCD (int num1, int num2);
  3.  
  4. int main(void)
  5. {
  6. int a=0, b=0;
  7. int result;
  8. printf("Please enter two numbers to calculate the greatest common denominator fromn");
  9. scanf("%d%d", &a, &b);
  10. result = greatestCD (a,b);
  11. printf("Using the correct in main way:nThe greatest common denominator of %d and %d is %dn",a,b, result);
  12. }
  13.  
  14. int greatestCD (int num1 ,int num2)
  15. {
  16.  
  17. if (num2==0){
  18. printf("Using the cheaty in gcd function way:nThe greatest common denominator is %dn",num1);
  19. return num1;
  20. } else {
  21. greatestCD(num2,(num1%num2));
  22. }
  23. }
  24.  
  25. C:UsersSamDocumentsC programs>gcd
  26. Please enter two numbers to calculate the greatest common denominator from
  27. 12
  28. 15
  29. Using the cheaty in gcd function way:
  30. The greatest common denominator is 3
  31. Using the correct in main way:
  32. The greatest common denominator of 12 and 15 is 2293524
  33.  
  34. greatestCD(num2,(num1%num2));
  35.  
  36. return greatestCD(num2,(num1%num2));
  37.  
  38. /dev/shm $ gcc x.c
  39. /dev/shm $ ./a.out
  40. Please enter two numbers to calculate the greatest common denominator from
  41. 12
  42. 15
  43. Using the cheaty in gcd function way:
  44. The greatest common denominator is 3
  45. Using the correct in main way:
  46. The greatest common denominator of 12 and 15 is 3
  47. /dev/shm $ gcc --version
  48. gcc (Debian 4.3.2-1) 4.3.2
  49. Copyright (C) 2008 Free Software Foundation, Inc.
  50. This is free software; see the source for copying conditions. There is NO
  51. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Add Comment
Please, Sign In to add comment