/* Let the user type in a number n. Then output n!. */ #include #include long factorial(long n); int main() { long n; printf("Please type in a number and press RETURN.\n"); scanf("%ld", &n); printf("%ld\n", factorial(n)); return EXIT_SUCCESS; } /* Return n!. */ #if 0 long factorial(long n) { long i; long product = 1; for (i = 1; i <= n ++i) { /* Why not start at zero? */ product *= i; } return product; } #endif #if 0 long factorial(long n) { if (n <= 1) { return 1; } return n * factorial(n - 1); } #endif long factorial(long n) { return n <= 1 ? 1 : n * factorial(n - 1); }