Calculate the factorial of a non-negative integer. The factorial of n (written as n!) is the product of all positive integers less than or equal to n. By definition, 0! = 1 and 1! = 1. Return null for negative numbers and null input.
Examples:
Input: n = 5
Output: 120
Explanation: 5! = 5 x 4 x 3 x 2 x 1 = 120
Input: n = 0
Output: 1
Explanation: By definition, 0! = 1
Input: n = 1
Output: 1
Explanation: By definition, 1! = 1
Input: n = 10
Output: 3628800
Explanation: 10! = 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 3628800
Input: n = -1
Output: null
Explanation: Negative numbers do not have factorials
Input: n = null
Output: null
Explanation: Null input returns null
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay August 2026
Contest runs August 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.