1. ## Would you check my code, please?

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!

2. It gets caught in an infinite loop depending upon your input, and even when it doesn't get caught, it returns the wrong answer. For example, with inputs N = 46, D = 146, it finishes, but it thinks the gcdCN is 50. It should be 2. An input that gets caught in the infinite loop is N = 2, D = 2. You might want to try again on your gcd algorithm and learn to use code tags.

3. And Welcome to the forum!

4. Originally Posted by pianorain
It gets caught in an infinite loop depending upon your input, and even when it doesn't get caught, it returns the wrong answer. For example, with inputs N = 46, D = 146, it finishes, but it thinks the gcdCN is 50. It should be 2. An input that gets caught in the infinite loop is N = 2, D = 2. You might want to try again on your gcd algorithm and learn to use code tags.
Thanks! I was thinking so, but I couldn't find the prob. Now that I had some rest,
I noticed that
Code:
c=int(a-b)
could be zero then it starts the infinite loop.

Sorry for my lack of touch I couldn't find the code tags.

Thanks again.

5. on the abs function, you could simplify that.

You don't really need the 'A' variable. Just use the if and return either a or -a

6. I just thought I didn't want to change the variable's value, I guess C doesn't change it after all. Thanks.

7. You're not. That 'a' variable is not the same variable as the one in the previous function, its only local to the abs() function. In c the parameters are copies of the variables that you passed.

8. just a word of advice you have a getchar() after a scanf() which I think is to pause the program. You dont need that because scanf already "pauses" the program.

And welcome to the forum

9. Here's an algorithm in case you can not find one.
Euclidean algorithm - Wikipedia, the free encyclopedia

Note: I suggest changing

Code:
int gcdCN = gCD(N,D);
To two lines and adding the use of abs function to the call to prevent problems later for negative input.
Note: My code for Euclidean algorithm assumed non-negative input; your solution might not need abs on call.
Some C compilers will not like the declare and statement like you did it.

Code:
int gcdCN;
Code:
gcdCN = gCD(abs(N),abs(D));
Tim S.