How do I find a factorial?

Kraken picture Kraken · Mar 10, 2010 · Viewed 45.4k times · Source

How can I write a program to find the factorial of any natural number?

Answer

Kyle Rosendo picture Kyle Rosendo · Mar 10, 2010

This will work for the factorial (although a very small subset) of positive integers:

unsigned long factorial(unsigned long f)
{
    if ( f == 0 ) 
        return 1;
    return(f * factorial(f - 1));
}

printf("%i", factorial(5));

Due to the nature of your problem (and level that you have admitted), this solution is based more in the concept of solving this rather than a function that will be used in the next "Permutation Engine".