Fraction simplifier prog.

hi all,
i made a fraction simplifier prog.

but the code doesnt look gud to me.
i will greatly appreciate any diff method for fracion solving
using simple loops and if elses nothing advance than that.

thanx in advance!
[compiled on dev c++ 4.9.9.2]


//Authur: ^|X4X|^ Nation
//Email: cyberstudent99@yahoo.com
#include <iostream.h>
#include <conio.h>

main()
{
int numerator, denominator;
int count=2;
cout << "Enter Numerator & Denominator: ";
cin>>numerator>>denominator;
if (numerator%denominator==0)
{
numerator=numerator/denominator;
denominator=1;
}
else if (denominator%numerator==0)
{
denominator=denominator/numerator;
numerator=1;
}
else
{
while (count <=100)
{
if(numerator%count==0&&denominator%count==0)
{
numerator/=count;
denominator/=count;
count--;
}
count++;
}
}
cout << "Simplifed Fraction Is: " << numerator << " / " << denominator;

getche();
}
Last edited on
well, you could write
1
2
3
4
5
6
7
8
9
10
//I skiped your "if (numerator%denominator==0)" part becouse the folowing code already covers that
for(int count = numerator<denominator ? numerator : denominator; count>1; count--){
    // your code would not simplify 303 / 202 becouse it only looked to divisors up yo 100
    // yet max divisor may be the up to the lesser of the two numbers;
    if(numerator % count == 0 && denominator % count == 0){
        numerator /= count;
        denomirator /= count;
        break;//added break so thar division is performed only once. note that in this code count is decreasing.
    }
}
Last edited on
Google "greatest common divisor", or else look it up on wikipedia. Wikipedia gives you C code
to compute the GCD. To reduce a fraction a/b, you simply divide both a and b by GCD( a, b ).
thanx all 4 help
Topic archived. No new replies allowed.