# Thread: Reducing Fractions/Entering fraction form

1. ## Reducing Fractions/Entering fraction form

Is the only way to enter fractions in and scan them from the user is one number at a time as in, enter the numerator of fraction 1, enter the denominator of fraction 1 and have a scan statement in between those. I have my program written, but I couldn't think of a way to try and enter in one whole fraction at a time in the form, a/b. Also, how can I reduce the fractional answer? I have to write a program that adds two fractions, a/b + c/d. However, if the answer comes out, say 40/25 or 6/8, how could I write the code for reducing fractions. It has been a while since I have done any C programming and I have forgotten some of it.

2. Reducing a fraction is simple enough if you're allowed to have a floating point value at the end. Just divide the numerator by the denominator. If you have to change it back to a fraction then you might want to take another route like finding the gcd of each value.
Code:
```#include <stdio.h>

int main()
{
double d = 40.0 / 25;
printf( "%f\n", d );
return 0;
}```

3. It must be in fractional form though. The way you have it would be in decimal form. thanks though.

4. That's what I said, it works if you are allowed to have a floating point value at the end. Since you can't you need to look up how to find the greatest common divisor of both the numerator and denominator then divide each by the result to simplify the fraction. Take 5/15 for example, the gcd is 5 so you divide both numbers by 5 and you get the reduced fraction.

5 / 5 = 1
15 / 5 = 3

5. The easiest way to reduce fractions is to use Euclid's Algorithm. The algorithm finds the greatest common divisor of two numbers, dividing each of the original numbers by the gcd puts the fraction in its reduced form.

The algorithm works as follows, suppose the two numbers are 200 and 156,

200 = 1*156 + 44 (1)
156 = 3 * 44 + 24 (2)
44 = 1 * 24 + 20 (3)
24 = 1 * 20 + 4 (4)
20 = 5 * 4 + 0 (5)

At each iteration you find the remainder on dividing the larger number by the smaller, i.e. (1) the remainder of dividing 200 by 156 is 44, (2) the remainder of dividing 156 by 44 is 24 ...

Continue this until the remainder is zero, step (5), and the gcd is the previous remaider, in this case the remainder at step (4) which is 4.

Then 156/200 = (156/4) / (200/4) = 39/50
and 200/156 = (200/4) / (156/4) = 50/39

Hope that is some help.

Andrew