Simplifying fractions in c++

This is a discussion on Simplifying fractions in c++ within the C++ Programming forums, part of the General Programming Boards category; Gday, Just a quick question for anyone who knows. How do u simplify fractions. For example. If the output is ...

  1. #1
    Unregistered
    Guest

    Thumbs up Simplifying 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

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    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
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    66
    How do u simplify fractions. For example. If the output is 40/ 160 how do i get the compiler to print out 1/4???
    Wouldn't there be a quick way of perfoming a division and then converting the result to a fraction? Because 1/4 and 40/160 both result in 0.25.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Just write a gcd algorithm and then divide the numerator
    and denomerator by it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  2. Algebraic Fractions
    By treenef in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 04:10 AM
  3. Greatest Common Factors of Fractions
    By sonict in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 03:33 AM
  4. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 06:51 AM
  5. Decimals to Fractions
    By ToasterPas in forum C++ Programming
    Replies: 4
    Last Post: 12-28-2001, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21