# Reducing rational numbers - code not working properly

• 04-17-2009
Reducing rational numbers - code not working properly
I have a problem with reducing two rational numbers in the form a/b and need to recude them to form where a and b do not have any common integer dividers.

I tried something, but it didn't work. Here is the code:

Code:

```#include <stdio.h> int main() { int i, j, k; int a, b; scanf("%d/%d", &a, &b); if(a > b) j = b; else j = a; for(i = j;i > 1; i--) { if(((a % j) == 0) && ((b % j) == 0)) { k = j; break; } } a = a / j; b = b / j; printf("%d/%d\n", a, b); return 0; }```
It always gives me result 1/1. Where is the problem ? I really can't figure it out.
• 04-17-2009
Ideswa
You don't actually calculate something. You loop a or b times (depending which of the two is the largest number). If you have found a common divisor, you give assign k the value j which is pointless, because j is the largest of a and b, I think you want to do k = i;

You break out of the loop and you don't use k. Your algorithm isn't functioning. Maybe take a look at Euclidean algorithm - Wikipedia, the free encyclopedia, the "Greatest Common Divisor" algorithm, see if you can use that.
• 04-17-2009
BEN10
Quote:

```#include <stdio.h> int main() { int i, j, k; int a, b; scanf("%d/%d", &a, &b); if(a > b) j = b; else j = a; for(i = j;i > 1; i--) { if(((a % j) == 0) && ((b % j) == 0)) { k = j; break; } } a = a / j; b = b / j; printf("%d/%d\n", a, b); return 0; }```