-
Notifications
You must be signed in to change notification settings - Fork 0
/
FractionConverter.java
49 lines (37 loc) · 1.31 KB
/
FractionConverter.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 18 July 2022 6:30pm
//Araav Nayak
import java.util.Scanner;
public class fractionConverter {
private static Scanner s;
public static void main(String[] args) {
s = new Scanner(System.in);
String query = "";
System.out.println("This program converts decimals to fractions reduced to simplest form.\nPress 'exit' to exit. Enter a decimal below to begin:");
while(!query.equals("exit")) {
query = s.next();
if(!query.equals("exit")) System.out.println(deciToFrac(query) + "\n");
}
System.out.println("--> Exited succesfully.");
s.close();
}
public static String deciToFrac(String d) {
//handle exception cases
if(d.indexOf("/") != -1) return "already a fraction";
if(d.indexOf(".") == -1 || d.indexOf(".") >= d.length()-1) return d + "/1";
int deciPlace = d.substring(d.indexOf(".")+1).length(); //1
//make the fraction
int numerator = (int)(Double.parseDouble(d)*Math.pow(10, deciPlace));
int denominator = (int)Math.pow(10, deciPlace);
//find gcf of the numerator and denominator
int min = numerator;
if(denominator < numerator) min = denominator;
int gcf = 1;
for(int i = 1; i <= min; i++) {
if(numerator % i == 0 && denominator % i == 0) {
if(i > gcf) gcf = i;
}
}
//output result
return "--> " + numerator/gcf + "/" + denominator/gcf;
}
}