When you run below code
The
count
value becomes -1 and the program ends up with divide by zero exception.When you uncomment the
sysout
, it runs fine. Not sure howsysout
makes a diference.
public class HelloWorld{
public static void main(String []args){
int num = 1;
int count;
int sum;
int fact;
for(count=1,sum=0,fact=1;fact<=num;count=count+1){
//System.out.println("num:"+num+" fact:"+fact+" count:"+count+" sum:"+sum);
if(num%count==0){
fact = num/count;
sum = sum+fact;
System.out.println("num:"+num+" fact:"+fact+" count:"+count+" sum:"+sum);
}
}
}
}
Output:
num:1 fact:1 count:1 sum:1
num:1 fact:-1 count:-1 sum:0
Exception in thread "main" java.lang.ArithmeticException: / by zero at HelloWorld.main(HelloWorld.java:14)