Java range int?

I understand that int range in Java should be from -2 ^ 31 to 2 ^ 31-1. But when I run this piece of code 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;
    }
}

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

That doesn't make sense with 13. It looks like it's out of range and cool. What's wrong? Is this because I'm using Eclipse?

Although I think this is 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);
    }

}
0
source share
5 answers

Here I would like to mention the concept of integer hours. Maximum and minimum values ​​for int in java: int MAX_VALUE = 2147483647 int MIN_VALUE = -2147483648

check the following results.

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

Conclusion:

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

This shows that when you go beyond the + ve range of an integer, the following values ​​start again with a negative initial value.

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

13, 6227020800. java. ,

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

, 13 1932053504. .

. .

+18

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html:

int - 32- . -2 147 483 648 2 147 483 647 (). , (, ) - . , , , , , long .

: !

+3

Java Integer, :

int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648

, , (Factorial of 13) 1932053504 * 14 27048749056, int MAX_VALUE Factorial of 14. , , long.

+2

13 - 6227020800. 31 , .

(, ), BigInteger, .

+1

:

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

, , .

0

All Articles