1 import java.io.*;
2
3 public class ReduceFraction {
4
5 static int numerator, denominator;
6 static int GCM;
7
8 public static void main(String args[]) throws IOException {
9 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10
11 int tryAgain = 1;
12
13 System.out.println("Reduce Fraction");
14 System.out.println("===============");
15 System.out.println();
16 System.out.println("This program will reduce a fraction to its lowest terms.");
17 do {
18 System.out.println();
19 System.out.println("Enter the Numerator:");
20 numerator = Integer.parseInt(br.readLine());
21 System.out.println();
22 System.out.println("Enter the Denominator:");
23 denominator = Integer.parseInt(br.readLine());
24 System.out.println();
25 System.out.println("You've entered " + numerator + "/" + denominator);
26 GCMFinder(numerator, denominator);
27 System.out.println("GCM: " + GCM);
28 System.out.println("Lowest form: " + (numerator / GCM) + "/" + (denominator / GCM));
29 System.out.println();
30 System.out.println("Press 1 to input a new fraction.");
31 tryAgain = Integer.parseInt(br.readLine());
32 } while (tryAgain == 1);
33 }
34
35 public static void GCMFinder(int numerator, int denominator) {
36
37 for (int i = 1; i <= Math.abs(numerator); i++) {
38
39 if ((Math.abs(numerator) % i == 0) && (Math.abs(denominator) % i == 0)) {
40 GCM = i;
41 }
42
43 }
44
45 }
46 }
47