The greatest common divisor of two integers a and b, GCD(a,b) notboth of which i
ID: 3617234 • Letter: T
Question
The greatest common divisor of two integers a and b, GCD(a,b) notboth of which is zero, is the largest positive integer that dividesboth a and b. The Euclidean algorithm for finding this greatestcommon divisor of a and b is as follows:Divide a by b to obtain the integer quotation q and reminder r sothat a=bq+r. Note : if b=0, then GCD(a,b)=a.
Now procedure GCD(a,b)=GCD(b,r) so replace a with b and b with rand repeat this procedure.
Since the reminders are decreasing , eventually a reminder of 0will result. The last non zero remainder is GCD(a,b). Forexample,
1260=198x6+72 GCD(1260,198)=GCD(198,72)
198=72x2+54 =GCD(72,54)
72=54x1+18 =GCD(54,18)
54=18x3 +0 =18
Write a program that reads two integers and then calls a functionnames gcd that calculates and returns the greatest common divisor.Display the two integers and the greatest common divisor on theterminal.
Note: If either a or b is negative, replace it with its absolutevalue.
Explanation / Answer
please rate - thanks # include using namespace std; void gcd(int,int); int main() { int a,b,g; couta; coutb; coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.