- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

Given an integer number as input. The goal is to find the number of trailing zeroes in the factorial calculated for that number. A factorial of a number N is a product of all numbers in the range [1, N].

We know that we get a trailing zero only if the number is multiple of 10 or has a factor pair (2,5). In all factorials of any number greater than 5, we have a number of 2s more than 5s in prime factorization of that number. Dividing a number by powers of 5 will give us the count of 5s in its factors. So, the number of 5s will tell us the number of trailing zeroes.

**For Example**

number=6

Count of trailing zeros in factorial of a number are: 1

The factorial is 30. Prime factors of 30 : 2 * 3 * 5 So only one pair of (2,5) exists so trailing zeros is 1.

number=12

Count of trailing zeros in factorial of a number are: 2

The factorial is 479001600. Prime factors of 479001600 : 210 x 35 x 52 x 71 x 111 So we can get 2 pairs of (2,5) so trailing zeros are 2

**Approach used in the below program is as follows** −

In this approach we will divide the number by powers of 5. If it results in more than 1 then the number of trailing zeros will be the number of 5s. Add this to count.

Take an integer number as input.

Function trailing_zeros(int number) takes number and returns the count of trailing zeroes in factorial of number.

Take the initial count as 0.

Using a for loop, divide the number by powers of 5.

If number/i is greater than 1, add this value to count.

Return count as result at the end of loop.

#include <iostream> using namespace std; int trailing_zeros(int number){ int count = 0; for (int i = 5; number / i >= 1; i *= 5){ int temp = number / i; count = count + temp; } return count; } int main(){ int number = 50; cout<<"Count of trailing zeros in factorial of a number are: "<<trailing_zeros(number); return 0; }

If we run the above code it will generate the following output −

Count of trailing zeros in factorial of a number are: 12

- Related Questions & Answers
- Finding trailing zeros of a factorial JavaScript
- Count number of trailing zeros in product of array in C++
- C/C++ Program to Count trailing zeroes in factorial of a number?
- C/C++ Programming to Count trailing zeroes in factorial of a number?
- Count number of trailing zeros in Binary representation of a number using Bitset in C++
- Python Program to Count trailing zeroes in factorial of a number
- Java Program to Count trailing zeroes in factorial of a number
- Program to find trailing zeros in factorial of n in C++?\n
- C Program to count trailing and leading zeros in a binary number
- Factorial Trailing Zeroes in C++
- Count number of trailing zeros in (1^1)*(2^2)*(3^3)*(4^4)*.. in C++
- Remove Trailing Zeros from string in C++
- Maximum number of trailing zeros in the product of the subsets of size k in C++
- Add trailing Zeros to a Python string
- C program to find trailing zero in given factorial

Advertisements