This program for finding out GCD,LCM. I am facing problem when the program reach to while loop.My code is given below.
public class GCDLCM {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
int y = sc.nextInt();
int a,b;
if(x < y) {
a = y;
b = x;
while((a % b) != 0) {
a = b;
b = a % b;
}
System.out.println("GCD: "+b);
}
else {
a = x;
b = y;
while((a % b) != 0) {
a = b;
b = a % b;
}
System.out.println("GCD: "+b);
}
System.out.println("LCM: "+((x * y)/b));
}
}
ERROR: Exception in thread "main" java.lang.ArithmeticException: / by zero at basicProgrammin.GCDLCM.main(GCDLCM.java:24)
please help me, why my while loop is not working? Thanks in advance.