Factorial Trailing Zeros

Problem

Given a positive integer. Find the number of trailing zeros in the integer’s factorial. For example the factorial of 10 is 3628800 so the algorithm should print 2

Solution

Let us start by a simple example. Factorial of 5 = 5 x 4 x 3 x 2 = 5 x 2 x 2 x 3 x 2 = 10 x 12 = 120. As you can see each pair of 5 x 2 contributes one zero in the final result. The problem reduces to finding the number of 5 x 2 pairs. Please refer to the code below for more details.

Code

Here is the code in C++

Add a Comment

Your email address will not be published. Required fields are marked *