Given a non-negative integer n, return the number of trailing zeros in n factorial (n!). For example, 10 factorial equals 3628800 which has 2 trailing zeros. Trailing zeros come from pairs of factors 2 and 5, and since there are always more factors of 2 than factors of 5 in a factorial, you only need to count the factors of 5. Return null for null input. Return 0 for negative inputs.
Examples:
Input: n = 0
Output: 0
Explanation: 0 factorial is 1, which has no trailing zeros
Input: n = 5
Output: 1
Explanation: 5 factorial is 120, which has 1 trailing zero
Input: n = 10
Output: 2
Explanation: 10 factorial is 3628800, which has 2 trailing zeros
Input: n = 25
Output: 6
Explanation: Counting factors of 5: 25/5 + 25/25 = 5 + 1 = 6
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 June 2026
Contest runs June 1 - 30. Complete challenges to climb the leaderboard!
Only the 30 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.