d user- common divisor (GCD) of two positive integers is the largest integer div
ID: 3870152 • Letter: D
Question
d user- common divisor (GCD) of two positive integers is the largest integer divides the numbers exactly. For example, the GCD of 14 and given that the two numbers are called a and b, as GCD(a, b) = a, if b = 0 GCD(a, b) = GCD(b, a%b), if b > 0 Implement this recursive formula as a recursive C++ function, and write a driver 3. The greatest 1 is 7; for 13 and that end for 45 and 27, it is 9. We can write a recursive formula for finding the C2i ing the GCL that allows you to test it interactively.Explanation / Answer
Solution:
#include<iostream>
using namespace std;
int gcd(int a,int b)
{
if(b==0) // if b==0 then return a
return a;
else
return gcd(b,a%b); // recursive calling
}
int main()
{
int a,b;
cout<<"Enter first number:";
cin>>a;
cout<<"Enter second number:";
cin>>b;
cout<<"GCD of "<<a<<" & "<<b<<" is: "<<gcd(a,b)<<" ";
}
Output:
Enter first number:16
Enter second number:4
GCD of 16 & 4 is: 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.