-2

When you run below code

  1. The count value becomes -1 and the program ends up with divide by zero exception.

  2. When you uncomment the sysout, it runs fine. Not sure how sysout 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)

ItsPete
  • 2,363
  • 3
  • 27
  • 35
Kris
  • 719
  • 1
  • 11
  • 19
  • Are you certain the code you are showing generated this output? If so, can you enable the first print statement and re-generate the output? – Chris94 Feb 24 '20 at 01:31
  • I am pretty sure what happened here is your looped through all int values. 1%1 would be true, but all other values of 1%n are false. That is for positive values. Once you overflow the int, I don't believe 1%0 is zero, so you don't attempt to print. However, 1%-1 is zero I am guessing. (I haven't looked at the formal definition of Modulus in awhile, but I think it is something like that.) The bottom line is the "if" statement wasn't true on zero. However, that doesn't explain why system.out.println() changes the results. – Frank Merrow Feb 24 '20 at 05:13

2 Answers2

1

You keep incrementing count and it eventually overflows and wraps around to 0.

In binary, Integer.MAX_VALUE = 2147483647 = 01111111 11111111 11111111 11111111                                              
                                            ^
                                            sign bit (positive)

When you add one more to the number it becomes

          Integer.MIN_VALUE = -2147483648 = 1000000 000000 000000 000000 000000
                                            ^
                                            sign bit (negative)

So it wraps around and goes from Integer.MAX_VALUE to Integer.MIN_VALUE.

  for(int count=Integer.MAX_VALUE -10; count != Integer.MIN_VALUE + 10; count++){
         System.out.println("count = " + count);
  }

As you can see if you keep on adding 1 to count, it will eventually increment to 0 and you get the divide by 0 error.

You can read more about Two's Complement Representation on Wikipedia.

WJS
  • 36,363
  • 4
  • 24
  • 39
0

The count is defined as an integer when its value becomes bigger than Integer.MAX_VALUE its overflows and becomes negative.

As (1 % any number greater than 1) cannot be Zero, so the loop keeps continuing and the count value keeps growing and it overflows after Integer.MAX_VALUE loops.

Ravi Sharma
  • 160
  • 4