- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

In mathematics, factorial is a function that multiplies a given number by every positive integer less than or equal to that number. The result of this function is denoted by the symbol !. For example, the factorial of 5 is 5! = 5 x 4 x 3 x 2 x 1 = 120.

In this article, we will discuss how to write a Golang program to count the number of trailing zeros in the factorial of a number.

## Algorithm to Count Trailing Zeros in Factorial of a Number

The algorithm to count the number of trailing zeros in the factorial of a number is as follows −

Initialize a variable count to 0.

Divide the number by 5 and store the quotient in num.

If num is greater than 0, add num to count.

Divide num by 5 and store the quotient in num.

Repeat steps 3-4 until num is less than or equal to 0.

The value of count is the number of trailing zeros in the factorial of the original number.

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

Now, let's write the Golang program to count the number of trailing zeros in the factorial of a number using the algorithm above −

### Example

package main import "fmt" func countTrailingZeros(n int) int { count := 0 for i := 5; n/i >= 1; i *= 5 { count += n / i } return count } func main() { num := 10 fmt.Printf("The number of trailing zeros in the factorial of %d is %d", num, countTrailingZeros(num)) }

### Output

The number of trailing zeros in the factorial of 10 is 2

In this program, we first define a function called countTrailingZeros that takes an integer n as an argument and returns an integer. This function calculates the number of trailing zeros in the factorial of n using the algorithm we discussed earlier.

We then define a variable num and pass it to the countTrailingZeros function. We print the result of the function using fmt.Printf, which displays the number of trailing zeros in the factorial of num.

This means that the factorial of 10 has 2 trailing zeros. You can modify the value of num to test the program for other values.

## Conclusion

We have learned how to write a Golang program to count the number of trailing zeros in the factorial of a number. We discussed the algorithm for this program and implemented it using Golang. By following the steps provided, you can write a program that calculates the number of trailing zeros in the factorial of any number. This program can be useful in various mathematical applications where it is required to calculate the number of trailing zeros in a large factorial. Golang provides an efficient and easy-to-use platform for such mathematical computations.