simplification

simplifying fractions in Java

会有一股神秘感。 提交于 2019-11-29 05:32:09
My task is to develop a rational class. If 500 and 1000 are my inputs, then (½) must be my output. I have written a program on my own to find it. Is there another best way to find the solution, or my program is already the best one? public class Rational { public static void main(String[] args){ int n1 = Integer.parseInt(args[0]); int n2 = Integer.parseInt(args[1]); int temp1 = n1; int temp2 = n2; while (n1 != n2){ if(n1 > n2) n1 = n1 - n2; else n2 = n2 - n1; } int n3 = temp1 / n1 ; int n4 = temp2 / n1 ; System.out.print("\n Output :\n"); System.out.print(n3 + "/" + n4 + "\n\n" ); System.exit