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!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow 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 Jan 2026
Contest runs January 1 - 31. Complete challenges to climb the leaderboard!
Points are calculated for challenges completed during the contest period.