Program to find the GCD of two numbers using recursion in C++
Solution :
#include <iostream>
using namespace std;
int GCD(int a,int b)
{
while(a!=b)
{
if(a>b)
return GCD(a-b,b);
else
return GCD(a,b-a);
}
return a;
}
int main()
{
int a,b,ans;
cout<<"Enter two numbers ";
cin>>a>>b;
ans = GCD(a,b);
cout << "GCD of "<< a<<" and "<<b<<" is "<<ans;
return 0;
}
Output :
Enter two numbers 60 30 GCD of 60 and 30 is 30