Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The greatest common divisor (gcd) of two integers a and b is a positive integer

ID: 3622587 • Letter: T

Question

The greatest common divisor (gcd) of two integers a and b is a positive integer c such that c divides a, c divides b, and for any other common divisor d of a and b, d is less than or equal to c. (For example, the gcd of 18 and 45 is 9.) One method of finding the gcd of two positive integers (a, b) is to begin with the smaller (a) and see if it is a divisor of the larger (b). If it is, then the smaller is the gcd. If not, find the next largest divisor of a and see if it is a divisor of b. Continue this process until you find a divisor for both a and b. This is the gcd of a and b.

Write an interactive C++ program that will accept two positive integers as input and then print out their gcd. Enhance your output by printing all divisors of a that do not divide b. A sample run could produce an opening splash screen logo and the following statements:

*************************************

* Welcome to the GCD Program! *

* By: Dr. RAPJ (2010) *

*************************************

Enter two positive integers. 42 72

The divisors of 42 that do not divide 72 are:

42
21
14
7

The gcd of 42 and 72 is 6.



Also, allow the user to repeat the program or quit if they choose. For example, you could then show:

Would you like to repeat this program? Enter “Y” for Yes or “N” for No.

Y

Explanation / Answer

Dear, // Program that print Greatest Common Deviser of two integers #include // Allows program to perform input and output using std::cout; // program uses cout using std::cin; // program uses cin int functionGCD(int, int); // function prototype of functionGCD int main() // function main begins program execution { int a; // declare variable a int b; // declare variable b // prompt user for data cout > a >> b; // output the GCD value cout