data-structures · beginner · ~15 min
Compute n! with a base case and a self-call.
Implement:
long long factorial(int n);
Return n! computed recursively. factorial(0)=1. 0 <= n <= 20.
n in [0,20].
n!.
Use long long (20! overflows 32 bits).
#include <stddef.h>
/* n! computed recursively. 0<=n<=20. factorial(0)=1. */
long long factorial(int n){ (void)n; return 0; }
Missing base case (infinite recursion); recursing without multiplying by n.
0! and 1! are both 1.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.