diff --git a/maths/factorial.c b/maths/factorial.c new file mode 100644 index 0000000000..b7e8014abb --- /dev/null +++ b/maths/factorial.c @@ -0,0 +1,23 @@ +– +#include + +int main(void) { + long long n; + printf("Enter a non-negative integer: "); + if (scanf("%lld", &n) != 1) { + printf("Invalid input\n"); + return 1; + } + if (n < 0) { + printf("Factorial is not defined for negative numbers.\n"); + return 1; + } + + long long result = 1; + for (long long i = 2; i <= n; ++i) { + result *= i; + } + + printf("%lld! = %lld\n", n, result); + return 0; +}