I was reading about Euclid's algorithm to calculate GCD and found the following code:
#include <stdio.h>
int main()
{
int m, n;
scanf("%d%d", &n, &m);
if (n < 0) n = -n;
if (m < 0) m = -m;
while(n != 0)
{
int temp = n;
n = m % n;
m = temp;
}
if(m != 0)
printf("The gcd is %d\n", m);
return 0;
}
But I have some questions:
why if
n<0orm<0we letn=-norm=-mWhat if m==0 what should I return? The smallest possible GCD is 1 but this function doesn't return anything in this case... (If we ignore
return 0which is necessary for main)