Gday,

Just a quick question for anyone who knows.

How do u simplify fractions. For example. If the output is 40/ 160 how do i get the compiler to print out 1/4???

Thanx

Printable View

- 09-01-2002UnregisteredSimplifying fractions in c++
Gday,

Just a quick question for anyone who knows.

How do u simplify fractions. For example. If the output is 40/ 160 how do i get the compiler to print out 1/4???

Thanx - 09-01-2002skipper
Since this smells of a homework problem, I'm going to put

*your*mind back to work. :)

What common arithmetic operator can be used to determine if two values are multiples of one another? (Think zero remainder.)

Okay? Now, how could you create a loop to run through the possibilities until you've reduced both the numerator and denominator to their lowest values?

Hint: create a FOR loop that initializes the counter to the value of the numerator and**decrement**, testing each successive value as a potential common multiple of both the numerator and denominator. Note that you will want to stop your loop before you reach '0' i.e. x > 0, lest you blow up the program!

(The loop is trivial in that it requires only about five lines of code. I leave it to you to refine it to reduce execution time. That is, suppose your example were 40000/160000?)

Good luck!

-Skipper - 09-01-2002CrimpyQuote:

How do u simplify fractions. For example. If the output is 40/ 160 how do i get the compiler to print out 1/4???

- 09-01-2002Nick
Just write a gcd algorithm and then divide the numerator

and denomerator by it.