Add two fractions using Java
Add two fractions using java
In this article we will discuss the program for add two fractions using java. For this purpose we need to ask the user to enter two fractional values where each fraction must consist a Numerator and a Denominator.
Concept
For understanding this in a better way lets suppose an example :
Suppose, First fraction consist of 1 as numerator and 3 as denominator, and Second fraction consist of 3 as numerator and 9 as denominator.
(1 / 3) + (3 / 9) = (6 / 9) = (2 / 3)
- Find LCM of 3 and 9 and the result will be 9.
- Multiply 3 in first fraction : (3 / 9) + (3 / 9)
- Add both fractions and then the result will be : (6 / 9)
- Now simplify it by finding the HCF of 6 and 9 and the result will be 3.
- So divide 6 and 9 by 3 and then the result will be : (2 / 3)
- This will be your simplified answer for the given problem.
Algorithm
- Initialize variables of numerator and denominator
- Take user input of two fraction
- Find numerator using this condition (n1*d2) +(d1*n2 ) where n1,n2 are numerator and d1 and d2 are denominator
- Find denominator using this condition (d1*d2) for lcm
- Calculate GCD of a this new numerator and denominator
- Display a two value of this condition x / gcd, y/gcd
Java code
Run
//Java program to add two fractions import java.util.Scanner; public class Main { public static void main(String[] args) { //scanner class declaration Scanner sc = new Scanner(System.in); //input from the user System.out.print("Enter numerator for first fraction : "); int num1 = sc.nextInt(); System.out.print("Enter denominator for first fraction : "); int den1 = sc.nextInt(); System.out.print("Enter numerator for second fraction : "); int num2 = sc.nextInt(); System.out.print("Enter denominator for second fraction : "); int den2 = sc.nextInt(); int num, den, x; System.out.print("("+num1+" / "+den1+") + ("+num2+" / "+den2+") = "); //logic for calculating sum of two fractions if(den1 == den2) { num = num1 + num2 ; den = den1 ; } else{ num = (num1*den2) + (num2*den1); den = den1 * den2; } if(num > den) x = num; else x = den; for(int i = 1 ; i <= x ; i++) { if(num%i == 0 && den%i == 0) { num = num/i; den = den/i; } } //logic for getting simplified fraction int n = 1; int p = num; int q = den; if( num != den) { while(n != 0) { //storing remainder n = num % den; if(n != 0) { num = den; den = n; } } } System.out.println("("+p/den+" / "+q/den+")"); //closing scanner class(not compulsory, but good practice) sc.close(); } }
Output
Enter numerator for first fraction : 1 Enter denominator for first fraction : 3 Enter numerator for second fraction : 3 Enter denominator for second fraction : 9 (1 / 3) + (3 / 9) = (2 / 3)
For similar Questions click on the given button.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
public class Frac{
public static void main(String[] args){
int num1 = 4;
int deno1 = 5;
int num2 = 6;
int deno2 = 7;
int numerator = (num1 * deno2) + (num2 * deno1);
int denomerator = deno1 * deno2;
int resultN = numerator /gcd(numerator , denomerator);
int resultD = denomerator /gcd(numerator , denomerator);
System.out.println(resultN +”/” +resultD);
}
public static int gcd(int a , int b){
return b==0?a:gcd(b , a%b);
}
}
import java.util.*;
public class Main{
public static int lcm(int a,int b){
int l=a>b?a:b;
int la=0;
for(int i=l;i<=(a*b);i++){
if(i%a==0&&i%b==0){
la=i;
break;
}
}
return la;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n1,n2,d1,d2,n=0,d=0;
n1=sc.nextInt();
n2=sc.nextInt();
d1=sc.nextInt();
d2=sc.nextInt();
System.out.print("("+n1+"/"+d1+")+("+n2+"/"+d2+")=");
if(d1==0 || d2==0){
System.out.println("Invalid Fraction");
}
else{
d=lcm(d1,d2);
n1=n1*(d/d1);
n2=n2*(d/d2);
n=n1+n2;
System.out.println("("+n+"/"+d+")");
}
}
}
import java.util.*;
public class Main
{
public static void main(String[] args) {
int a, b,c,d,x,y,i,gcd = 0;
Scanner sc = new Scanner(System.in);
System.out.println(“\nEnter the numerator for 1st number : “);
a = sc.nextInt();
System.out.println(“\nEnter the denominator for 1st number : “);
b = sc.nextInt();
System.out.println(“\nEnter the numerator for 2nd number : “);
c = sc.nextInt();
System.out.println(“\nEnter the denominator for 2nd number : “);
d = sc.nextInt();
x=(a*d)+(b*c); //numerator
y=b*d; //denominator
// Trick part. Reduce it to the simplest form by using gcd.
for(i=1; i <= x && i <= y; ++i)
{
if(x%i==0 && y%i==0)
gcd = i;
}
System.out.println(“\nThe added fraction is “+ x/gcd + “/” + y/gcd);
System.out.println();
}
}