1 import java.io.*;
 2 
 3 public class ReduceFraction {
 4 
 5     static int numerator, denominator;//stores numerator and denominator
 6     static int GCM;//stores greatest common multiple
 7 
 8     public static void main(String args[]) throws IOException {
 9         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// user input
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());//user enters numerator
21             System.out.println();
22             System.out.println("Enter the Denominator:");
23             denominator = Integer.parseInt(br.readLine());//user enters denominator
24             System.out.println();
25             System.out.println("You've entered " + numerator + "/" + denominator);
26             GCMFinder(numerator, denominator);//will find the GCM
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());//user enters whether or not they want to try again
32         } while (tryAgain == 1);
33     }//closes main body
34 
35     public static void GCMFinder(int numerator, int denominator) {//method that will find the GCM
36 
37         for (int i = 1; i <= Math.abs(numerator); i++) {//a loop that will find the GCM
38 
39             if ((Math.abs(numerator) % i == 0) && (Math.abs(denominator) % i == 0)) {
40                 GCM = i;
41             }
42 
43         }//closes GCM finder for loop
44 
45     }//closes GCMFinder method
46 }
47