The range of int in Java

I understand that the int range in Java should be -2^31 to 2^31-1.

But when I run this code snippet with 20:

public class Factorial {
    public int factorial(int n) {
        int fac=1;
        for (int i=1; i<=n; i++) {
            fac *= i;
            System.out.println("Factorial of " + i + " is: " + fac);
        }
        return fac;
    }
}

The output:

Factorial of 1 is: 1
Factorial of 2 is: 2
Factorial of 3 is: 6
Factorial of 4 is: 24
Factorial of 5 is: 120
Factorial of 6 is: 720
Factorial of 7 is: 5040
Factorial of 8 is: 40320
Factorial of 9 is: 362880
Factorial of 10 is: 3628800
Factorial of 11 is: 39916800
Factorial of 12 is: 479001600
Factorial of 13 is: 1932053504
Factorial of 14 is: 1278945280
Factorial of 15 is: 2004310016
Factorial of 16 is: 2004189184
Factorial of 17 is: -288522240
Factorial of 18 is: -898433024
Factorial of 19 is: 109641728
Factorial of 20 is: -2102132736

It's not making sense from 13 (13! = 6,227,020,800). It looks like it's out of range and wrapped around. What's wrong? Is it due to Eclipse that I'm using?

Though I think it's not relevant, here is the test code:

public class TestFac {

    public static void main(String[] args) {
        int n;
        Scanner sc = new Scanner(System.in);

        System.out.println("Input num you want to factorial: ");
        n = sc.nextInt();
        Factorial fac = new Factorial();
        fac.factorial(n);
    }
}

Solution 1:

Here I would like to mention the concept of integer clock.

The maximum and minimum values for int in Java are:

int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648

Please check the following results

 int a = 2147483645;
 for(int i=0; i<10; i++) {
    System.out.println("a:" + a++);
 }

Output:

a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642

It shows that when you go beyond the limit of the +ve range of integer, the next values starts from its negative starting value again.

 -2147483648,       <-----------------
 -2147483647,                        |
 -2147483646,                        |
  .                                  |
  .                                  |
  .                                  |    (the next value will go back in -ve range)
  0,                                 |
 +1,                                 |
 +2,                                 |
 +3,                                 |
  .                                  |
  .                                  |
  .,                                 |
 +2147483645,                        |
 +2147483646,                        |
 +2147483647     ---------------------

If you calculate the factorial of 13 it is 6227020800. This value goes beyond the int range of java. So the new value will be

        6227020800
      - 2147483647 (+ve max value)
   -----------------
Value = 4079537153
      - 2147483648 (-ve max value)
   -----------------
value = 1932053505
   -             1  (for zero in between -ve to +ve value)
  ----------------
Answer = 1932053504

So, in your answer, the factorial of 13 is becoming 1932053504. This is how integer clock works.

You can use long datatype instead of integer to achieve your purpose.

Solution 2:

From Primitive Data Types:

The int data type is a 32-bit signed two's complement integer. It has a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type is generally the default choice unless there is a reason (like the above) to choose something else. This data type will most likely be large enough for the numbers your program will use, but if you need a wider range of values, use long instead.

Moral of the story: Never believe in your teacher blindly!

Solution 3:

If you check Java Integer, its maximum and minimum values are as follows:

int    MAX_VALUE = 2147483647
int    MIN_VALUE = -2147483648

If you do some maths, you will see that (factorial of 13) 1932053504 * 14 is 27048749056 which is beyond int MAX_VALUE and that's why you are getting the wrong result for the factorial of 14. So in order to have good results it is better to use the long type instead.

Solution 4:

Please run this code:

System.out.println("Minimum value of Integer is: " + Integer.MIN_VALUE);
System.out.println("Maximum value of Integer is: " + Integer.MAX_VALUE);

So you can see why it fails.