In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n.
So, you can use an 'unsigned' to don't have to deal with negative number.
Taking a 'ulong' you can calculate factorial until 20 (instead of 12 with a 'int' in your implementation).
If you want a larger range, try returning a 'decimal' or deal with 'BigInteger' from 'System.Numerics'.
The value of 0! is 1, according to the convention for an empty product.
You need catching this special case (what you dont), and set returning value according.
Now, we have to make the product of all numbers less than N:
n! = 1 * 2 * 3 * ... * (n-2) * (n-1) * n
We can reverse order, multiplication being commutative:
n! = n * (n-1) * (n-2) * ... * 3 * 2 * 1
or since *n * 1 = n*, we can reduce:
n! = n * (n-1) * (n-2) * ... * 3 * 2
or another notation:
n! = n * (n-1) * (n-2) * ... * (n-n+2)
static ulong factorial(ulong n)
ulong result = (n > 0) ? n : 1;
while (n > 1) result *= n--;