Count Trailing Zeros in Factorial in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main() {
    int n, count = 0;
    printf("Enter a number: ");
    scanf("%d", &n);
    for(int i = 5; n / i >= 1; i *= 5)
        count += n / i;
    printf("Trailing zeros in %d! = %d\n", n, count);
    return 0;
}

C Output

Input:
50

Output:
Trailing zeros in 50! = 12


C++ Program

#include <iostream>
using namespace std;
int main() {
    int n, count = 0;
    cout << "Enter a number: ";
    cin >> n;
    for(int i = 5; n / i >= 1; i *= 5)
        count += n / i;
    cout << "Trailing zeros in " << n << "! = " << count << endl;
}

C++ Output

Input:
100

Output:
Trailing zeros in 100! = 24


JAVA Program

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = sc.nextInt(), count = 0;
        for(int i = 5; n / i >= 1; i *= 5)
            count += n / i;
        System.out.println("Trailing zeros in " + n + "! = " + count);
    }
}

JAVA Output

Input:
25

Output:
Trailing zeros in 25! = 6


Python Program

n = int(input("Enter a number: "))
count, i = 0, 5
while n // i >= 1:
    count += n // i
    i *= 5
print(f"Trailing zeros in {n}! = {count}")

Python Output

Input:
10

Output:
Trailing zeros in 10! = 2


In-Depth Learning – Entire Concept in Paragraphs
Example
If we find 10! = 3628800, we can observe that it has two zeros at the end. This indicates that 10! contains two trailing zeros.

The Logic Behind It
A trailing zero is formed whenever a number is a multiple of 10. As 10 = 2 × 5, each time that we have a pair of a 2 and a 5 together, we form a trailing zero. There are lots of even numbers in a factorial (to give lots of 2s), but 5s are less common. Thus, the number of trailing zeros in n! depends on how many 5s there are in the prime factorization of n!.

Step-by-Step Thinking
Count the number of multiples of 5 that are less than or equal to n — every multiple of 5 contributes at least one 5 to the factorization.

But multiples of 25 (such as 25, 50, 75, etc.) contribute two 5s per multiple since 25 = 5 × 5.

Multiples of 125 contribute three 5s per multiple, and so on.

That's why we divide n by powers of 5 again and again until the quotient is zero.

Mathematical Formula
Number of trailing zeros in n! =
n / 5 + n / 25 + n / 125 +. (integer division)

Example for n = 50:
50/5 = 10
50/25 = 2
Total = 10 + 2 = 12 trailing zeros.

Real-Life Analogy
Suppose you are creating gift bags. One ribbon (2) and one bow (5) are used per gift bag. You have plenty of ribbons (2s) since nearly every number factorial yields a factor of 2, but bows (5s) are scarce. Whether there are complete gift bags or not (trailing zeros) hinges solely on the number of bows (5s). The more bows you have, the more complete gift bags.

Why It Matters
This is a traditional number theory problem and is often posed in coding interviews because:

It's a test of prime factor knowledge.

It tests your optimization ability — rather than calculating the factorial (which is gigantic), you apply maths to directly obtain the solution.

It's an excellent illustration of how pattern detection reduces heavy calculations.

Interview Relevance
In coding interviews, the employer enjoys this question since the naive algorithm of computing factorial and counting the zeros will not work for huge numbers (such as 1000!) because of an overflow. The mathematical solution holds good even for very large numbers (till billions) in an efficient way.

SEO Optimized Closing
Factorial trailing zeros is one of the programming concepts that mix mathematics and logical thinking. Rather than using brute force calculation, it conditions you to think in terms of patterns in factorization. If you are practicing for TCS, Wipro, or Infosys coding interviews, or working on competitive programming problem solutions, knowing this approach will improve you as a problem solver. Try this with various inputs such as 50!, 100!, and even 1000! to get the logic and the speed correct.