ccplusplus.com
Learn C, C++ Concepts
Wednesday, March 28, 2012
greatest common divisor of two numbers c++
/******************************************************************* * File : gcd.cpp * Author : Saurabh Gupta * Description : greatest common divisor of two numbers c++ * Date : PM 09:41 28 March 2012 * Source : http://www.ccplusplus.com/p/c_15.html * Note : *******************************************************************/ #include
using namespace std; 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; cout << "\nPROGRAM GCD C++"; cout << "\nEnter how many GCD computations? "; cin >> howMany; for (int i = 0; i < howMany; ++i) { cout << "\nEnter two integers: "; cin >> x >> y; cout << "\nGCD(" << x << ", " << y << ") = " << gcd(x, y) << endl; } return 0; } /* * OUTPUT * [sgupta@rhel6x64 cpp]$ c++ -o gcd gcd.cpp [sgupta@rhel6x64 cpp]$ ./gcd PROGRAM GCD C++ Enter how many GCD computations? 2 Enter two integers: 10 30 GCD(10, 30) = 10 Enter two integers: 100 400 GCD(100, 400) = 100 [sgupta@rhel6x64 cpp]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment