
Problem
Solution
Submissions
Factorial of a Number
Certification: Basic Level
Accuracy: 80%
Submissions: 10
Points: 5
Write a C++ function to find the factorial of a given non-negative integer using recursion.
Example 1
- Input: number = 5
- Output: 120
- Explanation:
- Step 1: Calculate 5! = 5 × 4 × 3 × 2 × 1.
- Step 2: Recursively calculate 5 × 4!.
- Step 3: Continue recursion until reaching the base case (0! = 1).
- Step 4: Return the final result 120.
Example 2
- Input: number = 0
- Output: 1
- Explanation:
- Step 1: By definition, 0! = 1.
- Step 2: This is the base case of the recursion.
- Step 3: Return 1 immediately.
Constraints
- 0 ≤ number ≤ 20
- Time Complexity: O(n)
- Space Complexity: O(n) due to recursion stack
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use recursion to calculate the factorial.
- Handle the base case where the number is 0 or 1.
- Ensure the function works for the upper limit of the constraint.