Mid Coding

Count Trailing Zeroes in Factorial of a Number?

int TrailingZeroes(int n)

int count = 0;

for (int i = 5; i <= n; i *= 5)

count += n / i;

return count;

Explanation:

Trailing zeros come from factors of 10 = 2 × 5, but 2s are plenty, count 5s.

More from C# Programming Tutorial

All questions for this course