Factorial of 4
Input
factorial(4)
Output
24
4! = 4 * 3 * 2 * 1 = 24; recursion reduces n until base case 0.
Full lesson preview
Implement recursion with a clear base case by writing a factorial function.
Problem statement
Task
Examples
Input
factorial(4)
Output
24
4! = 4 * 3 * 2 * 1 = 24; recursion reduces n until base case 0.
Input format
Output format
Constraints
Samples
Input
factorial(5)
Output
120
5! = 120