# simplifiing fractions part 2

• 01-14-2002
Unregistered
simplifiing fractions part 2
thanks everyone for your help, but i have a second question.... is there a way to simplify the fractions using loops? because that is about where i am in my programming. and i dont want to get ahead of my self. any assitance would be much appreciated thanks again.
• 01-14-2002
Nick
Code:

```int gcd(int a, int b) {     int r;     while (b != 0) {         r = a % b;         a = b;         b = r;     }              return a; }```
• 01-14-2002
Uraldor
this isn't the ideal solution, but it'll give you an idea of how to do it.

I have compiled this on MSVC++ 6.

Code:

```#include <iostream.h> #include <math.h> int main(void) {         int i = 0;         // we are assuming that numerator >= denominator         int numerator = 9;         int denominator = 12;         int simplified = 0;         int newNumerator = numerator;         int newDenominator = denominator;         while(!simplified)         {                 int i = 0;                 for(i = 2; i < newNumerator; i++)                 {                         // if they are both divisible by the number                         if(newNumerator % i == 0 && newDenominator % i ==0)                         {                                 // divide both by the number                                 newNumerator /= i;                                 newDenominator /= i;                                 // reset i to make sure that the exit condition is met properly                                 i = 0;                                 break;                         }                 }                 // if we've made it all the way through the loop, we have no more factors                 if(i == newNumerator)                 {                         simplified++;                 }         }         cout << "The simplifed version of " << numerator << "/" << denominator;         cout << " is " << newNumerator << "/" << newDenominator << endl;         return(0); }```
hope this helps
U.
• 01-14-2002
Uraldor
the greatest common divisor wasn't the answer he was looking for. didn't he want to simplify the fraction??
• 01-14-2002
Nick
Once you have the gcd all that's need to simplify is
Code:

```g = gcd(numer, denom) (numer / g) ----------- (denom / g)```
• 01-14-2002
Uraldor
i can't believe that i didn't see that the greatest common divisor is the way ...... i'm going to hang my head in shame after that!