# simplifiing fractions

• 01-14-2002
Unregistered
simplifiing fractions
Im just starting in C++, and im using borland 3.1. im tryin to create a program that simplifies fractions. i know im goin to have to use various loops, but im not sure how to go about the problem. do i turn it into a decimal then turn that to the fraction in simpilist form? anyones help would be greatly appreciated.
• 01-14-2002
Shiro
Assume N/M is your fraction, then you should search for the largest divisor of N and M. And N and M should be divided by that value, so you get P/Q where P=N/divisor and Q=M/divisor.

Example: 4/6, the largest divisor is 2. So your simplified fraction is 2/3.

A strategy to attack this problem is finding all divisors of N and all divisors of M. Then find the greatest divisor which is divisor of both N and M.

There are more sophisticated methods to do this, for example using recursion.
• 01-14-2002
Nick
Find the greatest common denominator by
using elucid's algorithm.

Code:

```int gcd(int a, int b) {     if (b == 0)         return a;     return gcd(b, a % b); }```
Then to simplify write a function which
makes a fraction from a numerator and denomerator.
Find the gcd of the numerator and denomerator and divide both by it.

struct Frac make_frac(int n, int d)
{
struct Frac f;
int g = gcd(n, d);

f.numer = n / g;
f.denom = d / g;
return f;
}
• 01-14-2002
Nick
Find the greatest common denominator by
using elucid's algorithm.

Code:

```int gcd(int a, int b) {     if (b == 0)         return a;     return gcd(b, a % b); }```
Then to simplify write a function which
makes a fraction from a numerator and denomerator.
Find the gcd of the numerator and denomerator and divide both by it.

Code:

```Frac make_frac(int n, int d) {     Frac f;     int g = gcd(n, d);         f.numer = n / g;     f.denom = d / g;     return f;    }```