From 7f24a647a1eb2222c284429cbb80216f28d734f6 Mon Sep 17 00:00:00 2001 From: soumyaa01 Date: Sat, 26 Oct 2019 21:18:54 +0530 Subject: [PATCH] Factorial implementation in C --- Algorithms/Maths/Factorial/factorial.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Algorithms/Maths/Factorial/factorial.c diff --git a/Algorithms/Maths/Factorial/factorial.c b/Algorithms/Maths/Factorial/factorial.c new file mode 100644 index 00000000..5a599571 --- /dev/null +++ b/Algorithms/Maths/Factorial/factorial.c @@ -0,0 +1,20 @@ +#include +int main() +{ + int n, i; + unsigned long long factorial = 1; + printf("Enter an integer: "); + scanf("%d",&n); + // show 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) + { + factorial *= i; // factorial = factorial*i; + } + printf("Factorial of %d = %llu", n, factorial); + } + return 0; +} \ No newline at end of file