Factorial in C, Objective-C
Example for versions
gcc 3.4.5,
gcc 3.4.5 (Objective-C),
gcc 4.2.4,
tcc 0.9.25
This example uses recursive factorial definition. Note that 13! and larger causes an overflow, so the last lines of the output look like this:
13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184
#include <stdio.h>
unsigned long long factorial(unsigned long long n)
{
if (n == 0) {
return 1;
} else {
return n * factorial (n - 1);
}
}
int main(void)
{
int n;
for (n = 0; n <= 16; n++) {
printf("%i! = %lld\n", n, factorial(n));
}
return 0;
}
Comments
]]>blog comments powered by Disqus
]]>