Count Number of Trailing Zeros in Factorial C Program

By | March 22, 2016

C Program To Count Number of Trailing Zeroes in Factorial of Number

Here’s a C Program To Count the Number of Trailing Zeros in Factorial of a Number. While Calculating Factorial of an Integer, you may get a Long Number ending with Multiple Zeroes. This Code Finds the Total Number of Zeroes at the End of a Factorial of any Number.

Example For Trailing Zeros in Factorial of Number

5! = 120
Number of Trailing Zeros = 1

50! = 30414093201713378043612608166064768844377641568960512000000000000

It is also expressed as 3.0414093201713E+64

Here, we have to consider Prime Factors of the Number of which the Factorial is being calculated. Remember, Trailing Zeros are always generated if the Number is a Prime Factor of 5 and 2.

Also Read: C Program To Find Prime Numbers

Method 1: C Program To Find Number of Trailing Zeros in Factorial of a Number using While Loop

#include<stdio.h>
 
int main()
{
    int num, count, total = 0;    
    printf("Enter an Integer:\t");
    scanf("%d", &num);
    count = 5;
    while((num/count) >= 1)  
    {   
        total = total + num/count;
        count = count * 5;
    }   
    printf("Total Number of Trailing Zeroes: \t%d", total);
    printf("\n");
    return 0;
}

Also Read: C Program To Calculate Factorial of a Number using Loops

 

Method 2: C Program To Count Number of Trailing Zeros in Factorial of an Integer using For Loop

#include<stdio.h>
 
int main()
{
        int num, count, total = 0;    
        printf("Enter an Integer:\t");
        scanf("%d", &num);
        for(count = 5; (num/count) >= 1; count = count*5)
        {
            total = total + num/count;
        }    
        printf("Total Number of Trailing Zeroes: \t%d", total);
        printf("\n");
        return 0;
}

Also Read: Calculate Factorial of Number using Recursion in C Programming

 

Output

C Program To Count Number of Trailing Zeros in Factorial of a Number

If you have any doubts or compilation errors in this Code to Count Number of Trailing Zeros in a Factorial of any Integer, let us know about it in the Comment Section below.

4 thoughts on “Count Number of Trailing Zeros in Factorial C Program

  1. Dipak Kale

    Can we find zeros at the end of a float number that has decimal points in it?

    Reply
    1. Vijay Kumar Sawant

      I think it is possible! You need to take in float values!

      Reply
  2. sinto das kollan

    brother ,input 3
    enter 6,count _zero 1
    enter 15 count _zero 3
    but when i print sum of count

    Reply

Let's Discuss