Trailing Zeros
Learn to write the code for counting the number of trailing zeros at the end of a factorial.
We'll cover the following
Challenge
Write a function called countTrailingZeros (int X)
that determines the number of trailing zeros at the end of X! (X factorial), where X is an arbitrary input from the user.
Note: In case of a negative number X, your program should return -1.
Example 1
Input: 12
12! = 479001600
Number of zeros at the end of 12! = 2
Example 2
Input: 5
5! = 120
Number of zeros at the end of 5! = 1
Example 3
Input: -20
Number of zeros at the end of -20! = -1
Coding exercise
Try to solve this challenge on your own. If you can’t solve it, you can look at the solution and explanation below.
Get hands-on with 1400+ tech skills courses.