ccplusplus.com
Learn C, C++ Concepts
Wednesday, March 28, 2012
c code to find gcd of two numbers
/******************************************************************* * File : gcd.c * Author : Saurabh Gupta * Description : c code to find gcd of two numbers * Date : PM 09:41 28 March 2012 * Source : http://www.ccplusplus.com/p/c.html * Note : *******************************************************************/ #include
int gcd(int m, int n) // function definition { // block begin int r; // declaration of remainder while (n != 0) { // not equal r = m % n; // modulus operator m = n; // assignment n = r; } // end while loop return m; // exit gcd with value m } int main() { int x, y, howMany, i = 0; printf ("\nPROGRAM GCD C"); printf ("\nEnter how many GCD computations?"); scanf ( "%d", &howMany); for (; i < howMany; ++i) { printf ("\nEnter two integers: "); scanf ("%d %d", &x, &y); printf ("\nGCD(%d, %d) = %d\n", x, y, gcd(x, y)); } return 0; } /* * OUTPUT [sgupta@rhel6x64 c]$ gcc -o gcd gcd.c [sgupta@rhel6x64 c]$ ./gcd PROGRAM GCD C Enter how many GCD computations?2 Enter two integers: 30 45 GCD(30, 45) = 15 Enter two integers: 15 10 GCD(15, 10) = 5 [sgupta@rhel6x64 c]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment