Here you will learn about how to count trailing zeros in factorial of number.
One simple approach to count trailing zeros is first find factorial of number and then count the zeros in the factorial one by one. It is fine for smaller number like 10.
10! = 3628800
So, trailing zeros = 2
But what about big numbers like 100. The factorial of 100 has 24 zeros in the end and almost 160 digits. Its really hard to store that big number and then count the zeros one by one.
There is a simple and very fast method to do this. We can count the zeros by counting the 5s in prime factor of n factorial.
Trailing zeros in n! = Count 5s in prime factors of n!
= floor(n/5) + floor(n/25) + floor(n/125) + . . . .
It is very frequently asked question in competitive programming. Let us see how this method can be implemented in C.
C Program to Count Trailing Zeros in Factorial of Number
printf("Enter a number:");
//repeatedly divide n by powers of 5 and update count
If you have any doubts regarding above tutorial then comment below.