forked from JGEC-Winter-of-Code/JWoC_Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EgyptianFractions.java
35 lines (34 loc) · 1.08 KB
/
EgyptianFractions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.*;
class EgyptianFractions
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int numerator,denominator,quotient;
System.out.println("Enter numerator and denominator ");
numerator=sc.nextInt();
denominator=sc.nextInt();
while(numerator>denominator)
numerator=numerator%denominator;
while(denominator>numerator && numerator>0 && denominator>0)
{
if(numerator==1)
{
System.out.println("1/"+denominator);
break;
}
else if(denominator==1)
break;
else
{
if(denominator%numerator==0)
quotient=denominator/numerator;
else
quotient=denominator/numerator+1;
System.out.println("1/"+quotient);
numerator=numerator*quotient-denominator;
denominator=denominator*quotient;
}
}
}
}