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:
res=n1/d1;
This can be stored in a variable (res) which is our desired output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.io.*; import java.util.Scanner; class calFraction { public static void main(String args[]) { double n1,d1,res= 0; Scanner input = new Scanner(System.in); System.out.println("\nEnter the numerator : "); n1 = input.nextDouble(); System.out.println("\nEnter the denominator for : "); d1 = input.nextDouble(); res=n1/d1;//fraction System.out.println("\nThe fraction is :"+ res); System.out.println(); } } |
Output:
1 2 3 4 5 6 7 |
Enter the numerator : 4 Enter the denominator for : 3 The fraction is :1.3333333333333333 |
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
res=x/y;
Finally, our desired output which is sum of two fractions is stored in the “res” variable which is also of type double.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.*; import java.util.Scanner; class calFraction { public static void main(String args[]) { double n1,d1,n2,d2,x,y,i,res; Scanner input = new Scanner(System.in); System.out.println("\nEnter the numerator for 1st fraction : "); n1 = input.nextDouble(); System.out.println("\nEnter the denominator for 1st fraction : "); d1 = input.nextDouble(); System.out.println("\nEnter the numerator for 2nd fraction : "); n2 = input.nextDouble(); System.out.println("\nEnter the denominator for 2nd fraction : "); d2 = input.nextDouble(); x=(n1*d2)+(n2*d1); //numerator y=d1*d2; //denominator res=x/y; System.out.println("\nThe addition fraction is :"+ res); System.out.println(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter the numerator for 1st fraction : 1 Enter the denominator for 1st fraction : 2 Enter the numerator for 2nd fraction : 3 Enter the denominator for 2nd fraction : 4 The addition fraction is :1.25 |
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
res=x/y;
The value stored in the variable (res) is our desired output which can be displayed in our output console screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.*; import java.util.Scanner; class calFraction { public static void main(String args[]) { double n1,d1,n2,d2,x,y,res = 0; Scanner input = new Scanner(System.in); System.out.println("\nEnter the numerator for 1st fraction : "); n1 = input.nextDouble(); System.out.println("\nEnter the denominator for 1st fraction : "); d1 = input.nextDouble(); System.out.println("\nEnter the numerator for 2nd fraction : "); n2 = input.nextDouble(); System.out.println("\nEnter the denominator for 2nd fraction : "); d2 = input.nextDouble(); x=(n1*d2)-(n2*d1); //numerator y=d1*d2; //denominator res=x/y;//subtraction fraction System.out.println("\nThe subtraction fraction is "+ res); System.out.println(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter the numerator for 1st fraction : 1 Enter the denominator for 1st fraction : 2 Enter the numerator for 2nd fraction : 3 Enter the denominator for 2nd fraction : 4 The subtraction fraction is -0.25 |
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
res=x/y;
Finally, the variable (res) is our desired output which, can be displayed on console screen using the System.out.println() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import java.io.*; import java.util.Scanner; class calFraction { public static void main(String args[]) { double n1,d1,n2,d2,x,y,i,res = 0; Scanner input = new Scanner(System.in); System.out.println("\nEnter the numerator for 1st fraction : "); n1 = input.nextDouble(); System.out.println("\nEnter the denominator for 1st fraction : "); d1 = input.nextDouble(); System.out.println("\nEnter the numerator for 2nd fraction : "); n2 = input.nextDouble(); System.out.println("\nEnter the denominator for 2nd fraction : "); d2 = input.nextDouble(); x=n1*n2; //numerator y=d1*d2; //denominator res=x/y; //subtraction fraction System.out.println("\nThe subtraction fraction is "+ res); System.out.println(); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Enter the numerator for 1st fraction : 2 Enter the denominator for 1st fraction : 3 Enter the numerator for 2nd fraction : 4 Enter the denominator for 2nd fraction : 5 The subtraction fraction is 0.5333333333333333 |