I have started this program to calculate the greatest common divisor. This is what I have so far:
#include <iostream>
#include <math.h>
using namespace std;
int getGCD(int a, int b)
{
a = a % b;
if (a == 0)
{
return b;
b = b % a;
}
if (b == 0)
{
return a;
}
}
int main()
{
int x, y;
cout << "Please enter two integers x and y, for GCD calculation" << endl;
cin >> x >> y;
cout << "The GCD of " << x << "and " << y << " is" << getGCD(x, y) << endl;
return 0;
}
I always get a 0 for the GCD. What am I doing wrong?
int g(int a, int b){ return b?g(b,a%b):a;}
– Solar