I think I've solved this , the only problem is the condition , where should the condition " in the gcd function " stop ?
I tried when r!=g but it does not work.
int x,y,i;
std::cin >> x; //You should check these numbers are non-zero integers.
std::cin >> y;
if(x % y == 0 && y < x)
{
return y;
}
elseif(y % x && x < y)//Don't forget the user might not have put them in order highest to lowest!
{
return x;
}
else
{
//Find the highest number.
if(x > y)
{
for(i = x - 1; i > 0; --i)//Starting with the highest number lower than x...
{
if(x % i == 0) //Is it a divisor of x?
{
if(y % i == 0) If yes, is it a divisor of y also?
{
return i; //If it is a divisor of both, it is the gcd.
}
}
}
//And do the same for case y > x...
}
but I think you mean do while ? since while and for work the same way ,
but " If g is a multiple of d, then d is the answer. " you mean divides ? , that's why I used the first if to determine it , since if it equals 0 then d would be the gcd,
but I got confused on the else part , I think that I did it right , but since there's no condition to suite the loop , what should I do ?
on the parts or lines " 27-28-29 " I used Euclid's algorithm.