Beginners Programs :

Java Program to Calculate Fractions – Addition, Subtraction, Multiplication

Java program to calculate the fractions of Addition, Subtraction, and Simple. If you have any doubts related to the code mentioned over here, leave a comment here.

Simple Fraction

Our problem statement here is to convert into fractions. To convert into fractions, we need a numerator and denominator. Therefore our inputs will be two double-type numbers i.e., numerator (n1) and denominator (d1). Our expected output will be of type double which is the fractional representation.

To take our necessary inputs at runtime, we can make use of Scanner class of Java. These help us read inputs from console screen at runtime for any primitive datatype just by creating an object for it in our class. So, by making use of nextDouble() method of Scanner class, we read our input numberator (n1) and denominator (d1). To find the fraction all we have to do is, to divide the numerator(n1) with the denominator(d1) as follows:


This can be stored in a variable (res) which is our desired output.


2. Fraction Addition

To perform addition on 2 fractions, we will need two numerators (n1 and n2) and two denominators (d1 and d2). Like above, we make use of Scanner class to read all the four inputs- n1, d1, n2 and d2.

res=n1/d1 + n2/d2

Since, denominators are different, we find the LCM and cross multiply as follows:

res= ((n1*d2)+(n2*d1))/ d1*d2

This will be our resultant output. So to arrive at this, we’ll to the calculation for both the numerator part and denominator part separately and store it in different variables. It’ll look something like below:

x=(n1*d2)+(n2*d1);  //numerator

y=d1*d2; //denominator


Finally, our desired output which is sum of two fractions is stored in the “res” variable which is also of type double.


3. Fraction Subtraction

To perform subtraction between two fractions, the method is similar as above. For 2 fractions, we require 2 numerators (n1 and n2) and denominators (d1 and d2). Using Scanner class in Java, we read all the inputs required for performing subtraction.

res= n1/d1 – n2/d2

res= ((n1*d2)-(n2*d1))/ d1*d2

To perform this we separate into two parts- numerator and denominator. After performing separate calculations, we divide the numerator part with denominator as follows:

x=(n1*d2)-(n2*d1); //numerator

y=d1*d2; //denominator


The value stored in the variable (res) is our desired output which can be displayed in our output console screen.


4. Fraction Multiplication 

To perform multiplication, we need two fractions which would mean, we require two numerators and two denominators. Using Scanner class, we gather all our required inputs at runtime. To perform multiplication,

res= (n1/d1) * (n2/d2)

res= (n1*n2) / (d1*d2)

But to make things simpler, we separately calculate the numerator part and denominator part and then, divide the numerator with denominator part as follows:

x=n1*n2; //numerator

y=d1*d2; //denominator


Finally, the variable (res) is our desired output which, can be displayed on console screen using the System.out.println() method.