Final code

Factorial is the product of all positive integers less than or equal to N.

N! = 1*2*3 … (n-2)*(n-1)*n.

As you can see the factorial grows exponentially (100! = 9.3326215444×10157). At N =13 we are out of Int range and at N=25 out of Long range. As an alternative we can use BigInteger, however I find it slow and cumbersome. A way to go around is to represent the out-of-range number as a string, which in turn is an array of chars (digits).

Try multiply two numbers on paper – say XX  and YY. You will multiply X1  with YY  and then X2  with YY – writing the products one under the other with 1 digit to the left. In fact you don’t need to multiply all numbers at once but rather break the bigger one into digits, multiply every digit with the next number in line and write down the products in a string (thus keeping the gigantic result as a string at all times).