Fraction problem in beginner programming assignment (Java 8)

Discussion in 'Java' started by jufu, Sep 29, 2018.

  1. jufu

    jufu

    Joined:
    Sep 29, 2018
    Messages:
    1
    Likes Received:
    0
    Hey all, I'm a Java newbie currently in my first programming course at university. I'm working on a programming assignment in which I have to display if a fraction is proper or improper, and display the fraction's most simplified form. I've got all bases covered except one; mixed numbers. Suppose the user inputs 10 for the numerator variable and 4 for the denominator variable. I need a way for the system to compute that result is 2.5, and then display it as 2 + 1/2. Likewise, I need my program to do this for any fraction that would result in a mixed number (12/5 is 2 + 2/5, 43/6 is 7 + 1/6, etc.). It should be said that I also need the simplest way to do this, so as to not get ahead of myself or the rest of the class. The instructor may not like that, she is sort of particular. Thanks to anyone who replies in advance!

    P.S. please do not over work yourself and write a whole program, I've truly got all other aspects of this program covered. This one part only eludes me because we haven't been taught this and I can't find a straight answer online.
     
    jufu, Sep 29, 2018
    #1
    1. Advertisements

  2. jufu

    John Joe

    Joined:
    Apr 25, 2017
    Messages:
    99
    Likes Received:
    14
    Location:
    Malaysia
    Simplifying fractions is easy if you can folow the steps:

    1. find gcd of both num and den, so you have gcd=GCDFind(gcd, num);
    2. now, if gcd==1, then the fraction cannot be simplified (it is already in simplified form).
    3. if gcd > 1, then newNum = num/gcd; and newDen = den/gcd;
    It's all you need I think.

    The steps can easily found here https://stackoverflow.com/a/6882644/5156075
     
    John Joe, Sep 30, 2018
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.