The following code has a problem that I can't solve:
#include <stdio.h>
#include <stdlib.h>
int factorial(long long int x) {
long long int temp;
temp = x - 1;
for (; temp > 0; temp--) {
x = x * temp;
}
return x;
}
int main() {
long long int x, fact;
while (1) {
printf("Please enter the number that you want to learn factoriel...\n(To quit press ctrl+c)\n");
scanf("%lld", &x);
if (x == EOF) {
break;
}
if (x >= 0) {
fact = factorial(x);
printf("Factorial of %lld is %lld\n", x, fact);
} else {
fact = 0;
printf("Factorial of %lld is %lld\n", x, fact);
}
}
printf("The Program has successfully terminated...\n");
return 0;
}
First of all, it works until 17 but at 17 it gives me a negative bunch of numbers as a result and a couple numbers later it gives me 0 as a result
How do I fix this?