Reducing Fractions/Entering fraction form

This is a discussion on Reducing Fractions/Entering fraction form within the C Programming forums, part of the General Programming Boards category; Is the only way to enter fractions in and scan them from the user is one number at a time ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Reducing Fractions/Entering fraction form

    Is the only way to enter fractions in and scan them from the user is one number at a time as in, enter the numerator of fraction 1, enter the denominator of fraction 1 and have a scan statement in between those. I have my program written, but I couldn't think of a way to try and enter in one whole fraction at a time in the form, a/b. Also, how can I reduce the fractional answer? I have to write a program that adds two fractions, a/b + c/d. However, if the answer comes out, say 40/25 or 6/8, how could I write the code for reducing fractions. It has been a while since I have done any C programming and I have forgotten some of it.

  2. #2
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Reducing a fraction is simple enough if you're allowed to have a floating point value at the end. Just divide the numerator by the denominator. If you have to change it back to a fraction then you might want to take another route like finding the gcd of each value.
    Code:
    #include <stdio.h>
    
    int main()
    {
    	double d = 40.0 / 25;
    	printf( "%f\n", d );
    	return 0;
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    22
    It must be in fractional form though. The way you have it would be in decimal form. thanks though.

  4. #4
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    That's what I said, it works if you are allowed to have a floating point value at the end. Since you can't you need to look up how to find the greatest common divisor of both the numerator and denominator then divide each by the result to simplify the fraction. Take 5/15 for example, the gcd is 5 so you divide both numbers by 5 and you get the reduced fraction.

    5 / 5 = 1
    15 / 5 = 3
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    1
    The easiest way to reduce fractions is to use Euclid's Algorithm. The algorithm finds the greatest common divisor of two numbers, dividing each of the original numbers by the gcd puts the fraction in its reduced form.

    The algorithm works as follows, suppose the two numbers are 200 and 156,

    200 = 1*156 + 44 (1)
    156 = 3 * 44 + 24 (2)
    44 = 1 * 24 + 20 (3)
    24 = 1 * 20 + 4 (4)
    20 = 5 * 4 + 0 (5)

    At each iteration you find the remainder on dividing the larger number by the smaller, i.e. (1) the remainder of dividing 200 by 156 is 44, (2) the remainder of dividing 156 by 44 is 24 ...

    Continue this until the remainder is zero, step (5), and the gcd is the previous remaider, in this case the remainder at step (4) which is 4.

    Then 156/200 = (156/4) / (200/4) = 39/50
    and 200/156 = (200/4) / (156/4) = 50/39

    Hope that is some help.

    Andrew

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing main form from functions in other classes
    By pj_martins in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2004, 09:27 AM
  2. help with fraction division function
    By trippedwire in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2004, 12:38 PM
  3. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 09:55 AM
  4. Overloading insertion and extraction problem
    By Curwa in forum C++ Programming
    Replies: 1
    Last Post: 01-15-2003, 09:20 PM
  5. reducing a fraction
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-13-2002, 08:56 PM

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