# Golang Program to Count Trailing Zeros in Factorial of a Number

Given an integer n, write a Go program to count the number of trailing zeros in the factorial of n.

**Examples:**

Input :7Output :1 Explanation: 7! = 5040, which has 1 trailing zeroInput :10Output :2 Explanation: 10! = 3628800, which has 2 trailing zeros

**Naive approach:** To count the trailing zeros in n!, one approach is to find the factorial of n and then count the number of trailing zeros using a loop and counting the number of (n%10==0)s. However, even for small numbers such as 20, the factorial value is large enough to cause integer overflow.

**Efficient Approach:** To count the number of trailing zeros, it is first required to understand what contributes trailing zeros in the factorial of a number.

The number 10 is responsible for the trailing zeros. The prime factorization of 10 is 2*5. In prime factorization of any number, the number of 2s is always greater than or equal to the number of 5s. Hence, it is only required to find the number of fives in the prime factorization of the number.

**Example 1:** When n = 7, the prime factorization of 7! is 2^4 * 3^2 * 5^1 * 7^1. The number of 5s in the prime factorization is 1. Hence, the number of trailing zeros in 7! is 1.

**Example 2:** When n = 10, the prime factorization of 10! is 2^8 * 3^4 * 5^2 * 7^1. The number of 5s in the prime factorization is 2. Hence, the number of trailing zeros in 10! is 2.

trailingZerosCount = floor(n/5) + floor(n/5^2) + floor(n/5^3) + …. + floor(n/5^k) where 5^k <= n

`// Golang Program to Count Trailing ` `// Zeros in Factorial of a Number ` `package main ` ` ` `import ( ` ` ` `"fmt"` `) ` ` ` `func trailingZeros(n ` `int` `) ` `int` `{ ` ` ` ` ` `// This function return the number of ` ` ` `// trailing zeros in the factorial of ` ` ` `// a number n. ` ` ` `// The count variable denotes the ` ` ` `// number of trailing zeros in n! ` ` ` `count := 0 ` ` ` ` ` `// This loop counts the number ` ` ` `// of occurrences of 5 and its ` ` ` `// powers in n! ` ` ` `// Starting with p = 5, the loop ` ` ` `// calculates floor(n/5) + floor(n/25) + ` ` ` `// floor(n/125) + ..... until n/p >0 ` ` ` `for` `p := 5; n/p > 0; p *= 5 { ` ` ` `count += n / p ` ` ` `} ` ` ` `return` `count ` `} ` ` ` `// Driver code ` `func main() { ` ` ` `n := 10 ` ` ` `fmt.Printf(` `"The number of trailing zeros"` `+ ` ` ` `" in %v! is %v"` `, n, trailingZeros(n)) ` `} ` |

*chevron_right*

*filter_none*

**Output:**

The number of trailing zeros in 10! is 2

## Recommended Posts:

- Count trailing zeroes in factorial of a number
- Number of trailing zeros in N * (N - 2) * (N - 4)*....
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Smallest number with at least n trailing zeroes in factorial
- Program for factorial of a number
- C Program for factorial of a number
- Python Program for factorial of a number
- Java Program for factorial of a number
- How to Count the Number of Repeated Characters in a Golang String?
- Count numbers having N 0's and and M 1's with no leading zeros
- Count digits in a factorial | Set 2
- Count Divisors of Factorial
- Count digits in a factorial | Set 1
- Trailing number of 0s in product of two factorials
- Count factorial numbers in a given range
- Number of trailing zeroes in base 16 representation of N!
- Number of trailing zeroes in base B representation of N!
- Format a number with leading zeros in PHP
- Find the last digit when factorial of A divides factorial of B
- Sum of divisors of factorial of a number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.