Every time I run this, I get an error that reads: "Exception in thread 'main' java.lang.ArithmeticException: / by zero". I'm not sure why this isn't working.
public static void Solve(long num){
for(int x = 1; x < num; x++){
if((num % x) == 0){ //error occurs here
System.out.println(x);
}
}
}
num is a long. When you compare an int with a long like x < num, the int will be promoted to a long. Assuming your num is big enough (bigger than the max value of an int), x will never reach it and your x++ will be executed. At some point, the value of x will overflow and become 0.
Since num is a long, if you choose it sufficiently large, x, which is only an int, will overflow. And when it does that, as an int it will be zero. And then you get zero-divide in the remainder operation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With