#include <iostream>
using namespace std;
//Implement function GCD return by reference (&)
int& gcd(int a, int b)
{
/*
gcd(a,b)
a=b*q+r1
b=r1*q1+r2
........
to rn=0
........
Example
gcd(18,12)
18=12*1+6
12=6*1+0
gcd=6
*/
while (a != 0 && b != 0)
{
if (a > b)
a %= b;
else
b %= a;
}
int ans = a + b;
return ans;
}
int main()
{
cout << "Enter numbers: ";
int a, b;
cin >> a >> b;
int gc = gcd(a, b);
cout << "GCD=" << gc << endl;
return 0;
}
Comments
Leave a comment