2 ways to add fractions in Java [Practical Examples]

In this article we will learn how to add fractions in java, fraction addition in programming has a certain technique, which is different than adding two simple datatypes, here we will learn how we can do that.

 

The logic to add fractions

In mathematics, the concept of rationalization is used to add two fractions i.e. before we add two fractions, we make sure that the denominator of both the fractions is the same, if not, the denominator and numerator of the fraction whose denominator is different. The common denominator is called the LCM and it is multiplied with both fractions. After this, the new numerators are then added. For example, if we want to add two fractions

Advertisement
⅓ +⅘

The denominators will be made the same

⅓*5 + ⅘ *3

New fractions become

5/15 + 12/15

And the answer will be

17/15

 

Java Environment

Before moving on, make sure you have the latest java version installed in your system. To check that type the following command on your windows command line.

java -version

And the output must be something like this

add fractions in java

If java is not recognized by your system, please install the latest java jdk visit oracle downloads for this.

Advertisement

 

How to add fractions in java

Now in Java, we first find the LCM of the two fractions. Then divide the LCM all the denominators and multiply the quotient by a fraction for a common denominator and then finally add them all up.

 

Add Fractions in Java using simple addition

First lets look at a code where we try to add  fractions in java  by simply adding them up

class addFractions {
    public static void main(String[] args) {
        double fraction1 = 1 / 2;
        double fraction2 = 1 / 3;
        double fraction3 = 1 / 4;
        double addFractions = fraction1 + fraction2 + fraction3;
        System.out.println(addFractions);
    }
}

The output is


As you can see, it does not add up right. Now let's add fractions in java using logic.

 

Add Fractions in Java using Cross Multiplication

We cross multiply the numerators and denominators and find the result. Like in the code below

class addFractions {
    public static void main(String[] args) {
        // initialise numerator of first fraction
        int numerator1 = 2;

        // initialise numerator of second fraction
        int denominator1 = 3;

        // initialise numerator of second fraction
        int numerator2 = 4;

        // initialise denominator of second fraction
        int denominator2 = 5;

        // use the cross multiplication rule
        int numerator3 = numerator1 * denominator2 + numerator2 * denominator1;

        // find lcm
        int denominator3 = denominator1 * denominator2;

        // print the results.
        System.out.println(numerator3 + "/" + denominator3);
    }
}

The output of this code is :

Advertisement
22/15

 

Add Fractions in Java Using Fractions Class

In java a class can be created for simplicity, in this part of code, we will create Fraction class and then add fractions.

Code

import java.util.*;
import java.lang.*;

class Fractions {
    int numerator;
    int denominator;

    public Fractions(int numerator, int denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }

    int getNumerator() {
        return numerator;
    }

    int getDenominator() {
        return denominator;
    }

    Fractions add(Fractions f) {
        int numerator = this.numerator * f.getDenominator() + f.getNumerator() * this.denominator;
        int denominator = this.denominator * f.getDenominator();
        return new Fractions(numerator, denominator);
    }

    void print() {
        System.out.println(numerator + "/" + denominator);
    }
}

class addFractions {
    // add fractions using Fraction class
    public static void main(String[] args) {

        // initialize fraction 1
        Fractions f1 = new Fractions(1, 2);

        // initialize fraction 2
        Fractions f2 = new Fractions(1, 3);

        // initialize fraction 3
        Fractions f3 = new Fractions(1, 4);

        // initialize fraction 4
        Fractions f4 = new Fractions(1, 5);

        // Add fractions 1 and 2
        Fractions f5 = f1.add(f2);

        // Add fractions 3 and 4
        Fractions f6 = f3.add(f4);

        // Add fractions 5 and 6
        Fractions f7 = f5.add(f6);

        // Print the result
        f7.print();
    }
}

The output of this code is :

154/120

 

 Conclusion

In this article different ways how to add fractions in java. First, we studied that normal addition cannot work with fractions, then we studied the cross multiplication technique, and then we created a java class to make the addition of fractions easier.

Further Reading

Escape Sequences
Java Text Block

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X