The greatest common divisor (gcd) of two integers a and b is a positive integer
ID: 3622332 • 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 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.
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.
Explanation / Answer
#include int functionGCD(int, int); int main() { int a; int b; printf("Enter two integers : "); scanf("%d %d", &a,&b); printf("The G.C.D. of %d and %d is %d ", a, b ,functionGCD(a,b)); system("pause"); } int functionGCD(int a, int b) { int gcd; if (a > b) { int temp = a; a = b; b = temp; } while(true) { int reminder = b % a; if (reminder == 0) { gcd = a; break; } else { b = a; a = reminder; } } printf("The numbers are %d %d " , a ,b); return gcd; } I hope this will helps to You !Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.