factorial


See On Github

Data

Contributor

Generic placeholder thumbnail

by harshasrisri

in c

Source Code

Factorial
=========

A recursive implementation to find the factorial of a number. 
The output is restricted by the range of unsigned long long in C, which is atleast 64 bits long.
Here is a sample output.

```
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000
```
#include <stdio.h>

unsigned long long factorial (int number) {
	if (0 == number) return 1;
	else return number * factorial (number-1);
}

int main (int argc, char **argv) {
	int input = argv[1] ? atoi (argv[1]) : 10;
	return printf ("%d! = %llu\n", input, factorial (input));
}