This is an integer overflow . Use long or unsigned long instead of int . (And, as @Dunes suggested, your best bet is really BigInteger when working with very large numbers, because it will never theoretically overflow)
The basic idea is that signed -2,147,483,648 to 2,147,483,647 store numbers between -2,147,483,648 to 2,147,483,647 , which are stored as binary bits (all information on the computer is stored as 1 and 0 )
Positive numbers are stored from 0 in the most significant bit, and negative numbers are stored from 1 in the most significant bit. If your positive number becomes too large in binary representation, the numbers will be transferred to the signed bit and turn your positive number into the binary representation of the negative.
Then, when the factorial becomes larger than even the one that can store the unsigned int , it will “turn around” and lose the transfer from its most significant (signed) bit - this is why you see a pattern of sometimes alternating positive and negative values in your output.
source share