// gcd.cxx /***************************** /* This program will calculate the /* greatest common devisor and /* least common multiple of two integers. /* /* Written by: Lisa Venezia /* Modified by: Amruth Kumar /*****************************/ #include #include using std::cout; using std::cin; using std::endl; int main() { int num1; // First number to be entered by the user int num2; // Second number to be entered by the user int gcd; // Greatest common divisor of the two integers int lcm; // Least common multiple of the two integers // Robustly read the first positive integer cout << "Please enter the first positive integer: "; cin >> num1; while( num1 <= 0 ) { cout << "Please re-enter a valid positive integer: "; cin >> num1; } // Robustly read the second positive integer cout << "Please enter the second positive integer: "; cin >> num2; while( num2 <= 0 ) { cout << "Please re-enter a valid positive integer: "; cin >> num2; } // Set the smaller integer to be the tentative gcd if( num1 < num2 ) { gcd = num1; } else { gcd = num2; } // Test all the numbers from gcd down to 1 // until a number is found which evenly divides both the input integers while( gcd > 1 && !( (num1 % gcd == 0) && (num2 % gcd == 0) ) ) { gcd--; } // Report if the two numbers are relative primes if( 1 == gcd ) { cout << num1 << " and " << num2 << " are relative primes\n"; } // Find the least common multiple lcm = ( num1 * num2 ) / gcd; // Print the results cout << "The greatest common divisor of " << num1 << " and " << num2 << " is " << gcd << endl; cout << "The least common multiple of " << num1 << " and " << num2 << " is " << lcm << endl;; return EXIT_SUCCESS; }