/****************************************************** ** Name: ** Filename: gcd.cpp ** Project #: Deitel & Deitel 3.45 ** Project Description: The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd that returns the greatest common divisor of x and y. The gcd of x and y is defined recursively as follows: If y is equal to 0, then gcd(x,y) is x; otherwise gcd(x,y) is gcd(y, x % y ), where % is the modulus operator. ** Output: Greatest common diviser of the two input integers ** Input: Two integers ** Algorithm: Print program purpose Instruct user to input two integers Use two input integers in the recursive gcd function If y == 0, then gcd(x,y) is x Else gcd(x,y) is gcd(y, x % y) Return the gcd of the two input integers End program ******************************************************/ // Include files #include // used for cin, cout #include using namespace std; // Global Type Declarations // Function Prototypes void instruct (void); void pause (); int gcd ( int, int ); //Global Variables - should not be used without good reason. int main () { // Declaration section int int1, int2; // Executable section instruct (); cout << "Enter first integer: "; cin >> int1 ; cout << "Enter second integer: "; cin >> int2; cout << "\nThe greatest common divisor of " << int1 << " and " << int2 << " is " << gcd(int1,int2) << endl; pause (); return 0; } int gcd ( int x, int y ) { if ( y == 0 ) return x; else return gcd( y, x % y ); } void instruct (void) { // Declaration section cout << "This program will take two numbers that are input and\n" << "return the greatest common divisor of the two numbers.\n" << "______________________________________________________\n" << endl; // Executable section } void pause () { // Declaration section // Executable section cout << "\nPress any key to continue..."; getch(); cout << "\r"; cout << " "; cout << "\r"; } /* Program Output This program will take two numbers that are input and return the greatest common divisor of the two numbers. ______________________________________________________ Enter first integer: 75 Enter second integer: 25 The greatest common divisor of 75 and 25 is 25 Press any key to continue... */