Recursive Factorial
Completed
Write a recursive int fact(int n) and print fact(n) for n from 0 to 12.
Input
One integer 0–12.
Output
n!
Example
Input
5
Output
120
Editor
C
Hints
Base case first: fact(0) = fact(1) = 1.
Solution explanation
Every recursive function needs a base case that stops the recursion.
Show solution
#include <stdio.h>
int fact(int n) {
if (n <= 1) return 1;
return n * fact(n - 1);
}
int main() {
int n;
scanf("%d", &n);
printf("%d\n", fact(n));
return 0;
}