Java Program to Count trailing zeroes in factorial of a number

To count trailing zeroes in factorial of a number, the Java code is as follows −


 Live Demo

public class Demo{
   static int trailing_zero(int num){
      int count = 0;
      for (int i = 5; num / i >= 1; i *= 5){
         count += num / i;
      return count;
   public static void main (String[] args){
      int num = 1000000;
      System.out.println("The number of trailing zeroes in " + num +" factorial is " +


The number of trailing zeroes in 1000000 factorial is 249998

A class named Demo contains a function named ‘trailing_zero’ that initializes count value to 0, and iterates through the number whose factorial’s number of zeroes need to be found. This count is returned as output from the function. In the main function, the value fro ‘num’ is defined, and this function is called by passing this number as a parameter. Relevant messages are displayed on the console.

Updated on: 08-Jul-2020


Kickstart Your Career

Get certified by completing the course

Get Started