Trailing Zeros in Factorial Java

PROGRAM TO COUNT TRAILING ZEROS IN FACTORIAL OF A NUMBER


import java.io.*;
  
class GFG 
{
    // Function to return trailing 
    // 0s in factorial of n
    static int findTrailingZeros(int n)
    {
        // Initialize result
        int count = 0;
  
        // Keep dividing n by powers 
        // of 5 and update count
        for (int i = 5; n / i >= 1; i *= 5)
            count += n / i;
  
        return count;
    }
      
    // Driver Code
    public static void main (String[] args) 
    {
        int n = 100;
        System.out.println("Count of trailing 0s in "
                                           n +"! is "
                                 findTrailingZeros(n));
    }
}

OUTPUT
Count of trailing 0s in 100! is 24 

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java