Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Compute factorial recursivelyE02. Generate first N Fibonacci numbers recursivelyE03. Find all subsets of a setE

Problem No 1

Compute factorial recursively

Easy

6 minute session

Summary

Write a recursive function to compute the factorial of a non-negative integer.

Problem statement

Given a non-negative integer n, return n! (n factorial) computed using recursion. Factorial of 0 is defined as 1. You should validate that n is an integer >= 0. For this exercise, assume inputs in tests are valid non-negative integers.

Task

Implement factorial(n) using recursion with proper base cases.

Examples

Example 1

Input

factorial(5)

Output

120

Explanation

5! = 5 × 4 × 3 × 2 × 1 = 120

Input format

A single integer n supplied as an argument to factorial(n).

Output format

An integer representing n!.

Constraints

0 ≤ n ≤ 20 (results fit in Python int in tests). Use recursion; do not use math.factorial.

Samples

Sample input 0

factorial(3)

Sample output 0

6

Explanation 0

3! = 6

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

03:45 PM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.