#include <stdio.h>
#include <stdlib.h>
//Function factorial with Premature Exit on Negative Data
//Computes n!
//n is greater than or equal to zero -- premature exit on negative data
int
factorial (int n)
{
int i, product;
if (n < 0) {
printf("\n***Function factorial reports ");
printf("ERROR: %d! is undefined***\n", n);
exit (1);
} else {
//Compute the product n x (n-1) x (n-2) x . . . x 2 x 1
product = 1;
for (i = n; i > 1; --i) {
product = product * i;
}
return (product);
}
}
int main (void) {
int n;
printf ("Enter a number: ");
scanf ("%d", &n);
printf ("%d! => %d\n", n, factorial(n));
return (0);
}