I'm solving a few exercises from HackerRank.com and the code works perfectly on Netbeans and even in the page compiler for test cases, but when I submit the code it throws me this error in every test(except the last):
ArithmeticException: thrown at Solution.main(Solution.java:15)
Here's the code:
Scanner s = new Scanner(System.in);
int a = s.nextInt(),j=1;
for(int i=0; i<a; i++){
int b = s.nextInt(), c =s.nextInt();
for(j = b*c; j>0;j--) {
if((b*c)%(j*j)==0){
System.out.println(b*c/(j*j));
break;}
}
}
Line 15 is:
if((b*c)%(j*j)==0){
What's wrong with the statement? I've set 'j' to be different from 0 in the for loop, so no reason for dividing by zero, which was the only explanation I could find by myself.
Thank you in advance.