Fraction problem in beginner programming assignment (Java 8)

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

  1. jufu


    Sep 29, 2018
    Likes Received:
    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. Advertisements

  2. jufu

    John Joe

    Apr 25, 2017
    Likes Received:
    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
    John Joe, Sep 30, 2018
    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.