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.

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

`⅓ +⅘`

The denominators will be made the same

`⅓*5 + ⅘ *3`

New fractions become

`5/15 + 12/15`

`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

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.

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;
}
}``````

The output is

`0`

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 :

`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;
}

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);
}
}

// 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

// Add fractions 3 and 4

// Add fractions 5 and 6

// 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.

Views: 28

Azka Iftikhar

She is proficient in multiple programming languages, including C++, Golang, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can read more on her GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

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

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com