How can I make this code shorter ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I make this code shorter ?

Code for simplifying fractions import java.util.Scanner; public class SimplifyingFractions { public static double smaller(double refer1,double refer2) { if(refer1<=refer2) { return refer1; } else { return refer2 ; } } public static void main (String[]args){ Scanner ask = new Scanner(System.in); double a,b ; a = ask.nextDouble(); b = ask.nextDouble(); double x = smaller(a,b); do { if(x !=1 && x==a && a%b==0 && a/b - Math.floor(a/b)==0) { int Ia,Ib; Ia = (int) a; Ib = (int) b ; System.out.println("(1) "+Ia/Ib); break; } else if(x !=1 && a<b && a%x==0 && b%x==0 && a/x-Math.floor(a/x)==0 && b/x-Math.floor(b/x)==0) { double r1,r2 ; r1 = a/x; r2 = b/x; int Ir1,Ir2; Ir1 = (int) r1; Ir2 = (int) r2; System.out.println("(2) "+Ir1+"/"+Ir2); break; } else if (x==a && a%b!=0 && a/x-Math.floor(a/x)!=0 && b/x-Math.floor(b/x)!=0) { int Ia,Ib; Ia = (int) a; Ib = (int) b ; System.out.println("(3) "+Ia+"/"+Ib); break; } } while(x<=a || x<=b) ; } }

31st May 2020, 7:07 AM
H-J
H-J - avatar
7 Answers
+ 1
@AJ Anant #19 Sep End Day I want to simplify fractions. The first condition in the loop is for improper,properly divisible fractions like 4/2(outout will be 2) The second condition is for simplifying proper fractions like 68/100 (output will be 17/25) The third condition is for proper fractions that can not be simplified like 3/7
31st May 2020, 8:10 AM
H-J
H-J - avatar
+ 1
instead smaller() use Math.min() // double x = smaller(a,b); double x = Math.min(a,b); but do {} while there is infinity loop because x,a,b are not changed inside it I try "5.25 8.75" as input for double variables
31st May 2020, 9:18 AM
zemiak
+ 1
@zemiak Thanks I'm a beginner .I understand that I need to use Math.min() but I don't understand the second point.
31st May 2020, 9:32 AM
H-J
H-J - avatar
1st Jun 2020, 12:22 AM
Sandra Meyer
Sandra Meyer - avatar
0
What you want to do here?
31st May 2020, 7:20 AM
A͢J
A͢J - avatar
0
if none of the if () expressions are true, the program never terminates
31st May 2020, 10:53 AM
zemiak
0
zemiak Thanks
31st May 2020, 10:54 AM
H-J
H-J - avatar