/* * Simple example of recursive function. */ #include int factorial(int n); /* function declaration */ int main(void) { int x; printf("enter integer\n"); if (scanf("%d", &x) != 1) { printf("bad input\n"); return 1; /* nonzero return means error */ } printf("x %d, fac x %d\n", x, factorial(x)); return 0; } /* * n! = n * (n-1) * (n-2) * ... * 1 * * n! = * if n == 0, 1 * otherwise n * (n-1)! */ int factorial(int n) { if (n <= 0) { return 1; } else { return n * factorial(n-1); } }