How this calculation works
There are more factors of 2 than 5 in a factorial, so the factors of 5 determine the number of final decimal zeros.
Sum floor(n/5ᵏ) for positive kA worked example
100! has 20 multiples of 5 plus four additional factors from multiples of 25.
Good to know
This counts zeros in base ten.
Reference: NIST DLMF, Functions of Number Theory (opens in a new tab) ↗
Count the factors of five in the factorial
A trailing decimal zero requires a factor of 10, or one factor each of 2 and 5. In a factorial, factors of 2 occur at least as often as factors of 5, so fives limit the count. For 100!, the multiples of 5 contribute 20 factors.
The multiples of 25 contribute an additional four fives because each contains at least 5². There are no multiples of 125 below 100. Adding the counts gives floor(100/5) + floor(100/25) = 20 + 4 = 24 trailing zeros. The number 25 is counted in both groups deliberately because it contributes two separate factors of five.
Do not count only visible multiples of ten
A factor of 10 can be formed by different factorial terms. In 5!, the separate factors 2 and 5 already produce one trailing zero even though none of the terms is 10. Counting only terms that end in zero would miss that contribution.
The Prime exponent in a factorial calculator generalizes the same repeated-division formula to a chosen prime. For decimal trailing zeros, selecting prime 5 gives the required count. Other numeral bases require balancing the prime factors of their base separately. The input is the factorial argument n, not an already computed factorial. This avoids constructing a huge product solely to inspect its final digits.
A few questions, answered
How many trailing zeros does 0! have?
Zero factorial equals 1, which has no trailing zeros.
Why does the count increase by two at 25?
The new factorial term 25 contributes two factors of five, increasing the zero count by two at once.