diff --git a/C/factorial.c b/C/factorial.c new file mode 100644 index 0000000..3a56c9f --- /dev/null +++ b/C/factorial.c @@ -0,0 +1,19 @@ +#include +int main() { + int n, i; + unsigned long long fact = 1; + printf("Enter an integer: "); + scanf("%d", &n); + + // shows error if the user enters a negative integer + if (n < 0) + printf("Error! Factorial of a negative number doesn't exist."); + else { + for (i = 1; i <= n; ++i) { + fact *= i; + } + printf("Factorial of %d = %llu", n, fact); + } + + return 0; +} \ No newline at end of file diff --git a/C/readme.md b/C/readme.md new file mode 100644 index 0000000..a04a1e6 --- /dev/null +++ b/C/readme.md @@ -0,0 +1,34 @@ +# Factorial + + + +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`. For example: + +``` +5! = 5 * 4 * 3 * 2 * 1 = 120 +``` + +| n | n! | +| ----- | --------------------------: | +| 0 | 1 | +| 1 | 1 | +| 2 | 2 | +| 3 | 6 | +| 4 | 24 | +| 5 | 120 | +| 6 | 720 | +| 7 | 5 040 | +| 8 | 40 320 | +| 9 | 362 880 | +| 10 | 3 628 800 | +| 11 | 39 916 800 | +| 12 | 479 001 600 | +| 13 | 6 227 020 800 | +| 14 | 87 178 291 200 | +| 15 | 1 307 674 368 000 | + +## References + +[Wikipedia](https://en.wikipedia.org/wiki/Factorial) \ No newline at end of file