1: // Filename: GCD_ITER.CPP 2: // Purpose: Finds the greatest common divisor (GCD) of pairs 3: // of positive integers. The GCD of each pair is 4: // output immediately after the input of the pair. 5: // The input of pairs is terminated by entering the 6: // end-of-file character. 8: #include <iostream> 9: using namespace std; 11: int GCD(/* in */ int a, /* in */ int b) 12: // Pre: Both a and b have been initialized with nonnegative integer 13: // values, and at least one of a or b is strictly positive. 14: // Post: The function value is the GCD of a and b. 15: { 16: int temp; 17: while (b != 0) 18: { 19: temp = b; 20: b = a % b; 21: a = temp; 22: } 23: return a; 24: } 26: int main() 27: { 28: cout << "\nThis program finds the greatest " 29: << "common divisor (GCD) of \n" 30: << "pairs of positive integers. \n\n"; 32: int first, second; 33: while (cin) 34: { 35: cout << "Enter two positive integers, " 36: << "separated by at least one \n" 37: << "space, followed by ENTER, " 38: << "or enter end-of-file to quit: \n"; 40: cin >> first >> second; 41: if (cin) 42: { 43: cout << "GCD(" << first << "," 44: << second << ") = " 45: << GCD(first, second) << "\n\n"; 46: } 47: } 49: return 0; 50: }