/* * Program demonstrating use of recursion to compute factorial of a number. * * Input: a positive integer n. * * Output: n! */ #include /* Compute n! and return it */ int factorial(int n) { int result; if (n <= 1) { result = 1; } else { result = n * factorial(n-1); } return result; } /* Main program */ int main(void) { int n; printf("enter a positive integer:\n"); if (scanf("%d", &n) !=1 ) { printf("error: not an integer\n"); } else if (n <= 0) { printf("error: negative or 0\n"); } else { printf("%d factorial is %d\n", n, factorial(n)); } return 0; }