needed in c++
(no step by step)
The gcd(m, n) can also be defined recursively as follows:
If m % n is 0, gcd (m, n) is n. Otherwise, gcd(m, n) is
gcd(n, m % n).
Write a recursive function to find the GCD. Write a test program that prompts the user to enter two integers and displays their GCD.
CODE IN C++:
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
int main()
{
int a, b ;
cout<<"Enter two numbers : ";
cin >> a >> b ;
cout << "Gcd of your numbers : " << gcd(a, b) <<
endl ;
return 0;
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.