Hello, I'm new to C programming and would appreciate if someone would help with this.
Every time I run the following the program "stops" at the line where the gCD function takes in values (I've tried it without this line and it doesn't stop).
Code:
int abs(int a)
{
int A=a;
if (a<0) A=-a;
return(A);
}
int gCD(int a, int b)
{
int gcd = 1;
while(a % 2 == 0 && b % 2 == 0)
{
gcd *= 2;
a /= 2;
b /= 2;
}
while (a % 2 == 0 || b % 2 == 0)
{
a /= 2;
b /= 2;
}
int c = abs(a - b);
while (c % 2 == 0)
{
c /= 2;
}
gcd *= c;
return(gcd);
}
int main()
{
int N;
int D;
printf("Type a numerator: ");
scanf("%d", &N);
getchar();
printf("\nType a denominator: ");
scanf("%d", &D);
getchar();
int gcdCN = gCD(N,D);
N /= gcdCN;
D /= gcdCN;
printf("Simplified numerator: %d\n", N);
printf("Simplified denominator: %d\n\n", D);
getchar();
return 0;
}
Thanks!