- 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

# C/C++ Programming to Count trailing zeroes in factorial of a number?

Counting the number of trailing zeroes in a factorial number is done by counting the number of 2s and 5s in the factors of the number. Because 2*5 gives 10 which is a trailing 0 in the factorial of a number.

## Example

Factorial of 7 = 5040, the number of trailing 0’s is 1.

Based on our logic 7! = 2*3*4*5*6*7, it has 3 2s and 1 5s so the number of trailing 0’s is 1.

#include <iostream> using namespace std; int main() { int n = 45; int count = 0; for (int i = 5; n / i >= 1; i *= 5) count += n / i; cout<<"No of trailing 0s in " << n<< "! is " << count; return 0; }

## Output

No of trailing 0s in 24! is 10

- Related Questions & Answers
- C/C++ Program to Count trailing zeroes in factorial of a number?
- Python Program to Count trailing zeroes in factorial of a number
- Java Program to Count trailing zeroes in factorial of a number
- Factorial Trailing Zeroes in C++
- Count trailing zeros in factorial of a number in C++
- Preimage Size of Factorial Zeroes Function in C++
- C++ Program to Find Factorial of a Number using Dynamic Programming
- Finding trailing zeros of a factorial JavaScript
- Find the Number of Trailing Zeroes in Base 16 Representation of N! using C++
- Find the Number of Trailing Zeroes in base B Representation of N! using C++
- Program to find trailing zeros in factorial of n in C++?
- Count number of trailing zeros in product of array in C++
- C program to find trailing zero in given factorial
- Count number of trailing zeros in Binary representation of a number using Bitset in C++
- C Program to count trailing and leading zeros in a binary number

Advertisements