Help with fraction problem
Hello. I'm trying to put a fraction in lowest terms, but i can't seem to get it working; I work it out on paper, everything seems it should be ok.
NOTE: den is for denominator, num is for numerator, gcd is for greatest common divisor. I start out with the number 2 for gcd and then test if that number can divide into the numerator and denominator evenly. If not, gcd is increased by one until it reaches den/2. if it does divide evenly into both, then num and den are both diveded by gcd and gcd is set to 1, whereon looping, setting gcd = 2, and repeating the process over agian.
Here's the code:
Code:
void Fraction::lowterms()
{
for(int gcd = 2; gcd <= den/2; gcd++ )
while(!(num%gcd) && !(den%gcd) )
{
num = num / gcd;
den = den / gcd;
gcd = 1;
}
}
It's really strange.
1) Sometimes the nothing is displayed on the screen ( i presume it's going into an infitite loops )
2) the numerator equates to some super high number like 185764 and the denominator is correct
3) the numerator equates to some super high number liek 185764 and the denominator is wrong ( but a low number );
i can't seem to figure out why... :confused: