Understanding limitations of int data-type

In a nutshell...

I am trying to find a solution for a question given to me in my programming course in college.

Given a small program, I am asked to explain **why for values 34 and higher, is the returned output always 0?**

*There are some strange logical errors that appear before this occurs, however they can be explained by int being a capable of only 32 bits and therefor cannot store a value greater than (positive/negative) 2147483648.*

So, first of all...

Here is the program

Code:

`public class Bonus01`

{

// uses a for-loop to multiply together the first n integers

public static int multFor(int n)

{

int retval = 1;

for(int i=1; i<=n; i++)

retval *= i; // equivalent to retval = retval * i

return retval;

}

// calls multFor for a range of integers from 1 to maxn

public static void callMultFor(int maxn)

{

for(int i=0; i<= maxn; i++) {

int currval = multFor(i);

System.out.println("multFor(" + i + ") = " + currval);

}

}

public static void main()

{

callMultFor(36);

}

}

Output of main()

Code:

`multFor(0) = 1`

multFor(1) = 1

multFor(2) = 2

multFor(3) = 6

multFor(4) = 24

multFor(5) = 120

multFor(6) = 720

multFor(7) = 5040

multFor(8) = 40320

multFor(9) = 362880

multFor(10) = 3628800

multFor(11) = 39916800

multFor(12) = 479001600

multFor(13) = 1932053504

multFor(14) = 1278945280

multFor(15) = 2004310016

multFor(16) = 2004189184

multFor(17) = -288522240

multFor(18) = -898433024

multFor(19) = 109641728

multFor(20) = -2102132736

multFor(21) = -1195114496

multFor(22) = -522715136

multFor(23) = 862453760

multFor(24) = -775946240

multFor(25) = 2076180480

multFor(26) = -1853882368

multFor(27) = 1484783616

multFor(28) = -1375731712

multFor(29) = -1241513984

multFor(30) = 1409286144

multFor(31) = 738197504

multFor(32) = -2147483648

multFor(33) = -2147483648

multFor(34) = 0

multFor(35) = 0

multFor(36) = 0

Making sense of this strange output...

A couple points provided in the question:

- Multiplying the value returned from multFor(13) by 14 appears to decrease the result;
- Multiplying the value from multFor(15) by 16 barely changes the result;
- Starting at multFor(17), we start to see negative numbers, despite the fact that we are only multiplying positive numbers together.

The above can be attributed to the limitations of the 32 bit sized int data-type.

What I cannot explain at this point

**Why the output is 0 starting at multFor(34) and on...** Please help!

I would love for anybody to shed some light on this phenomenon.

Thanks :)