Count Factorial Trailing Zeroes in java

In this tutorial, we will see how to count trailing zeros in factorial of a number in java.


Problem

Count number of zeros in factorial of number in java.

For example:
Factorial of 6 is 720, so a number of trailing zeros is 1.
Factorial of 14 is 87 178 291 200, so a number of trailing zeros is 2.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Solution

A very simple approach is to compute the factorial and divide it by 10 to count a number of trailing zeros but bound of ints will be reached very quickly with solution.

Trailing zeroes are created by multiple of 10 and multiples of 10 are created by 5 and 2. As multiple of 2 will always more than multiple of 5, we can simply count the multiples of 5.

Let’s create a simple program to count factorial Trailing Zeroes in java.

When you run above program, you will see below output.

Factorial trailing zeroes for 29: 6

This approach works but can we do better?

Yes,we can.


Algorithms to Count number of zeros in factorial of number in java

  • Divide the number by 5 and count the number of multiple of 5.
  • Divide the number by 25 and count the number of multiple of 25.
  • Divide the number by 125 and count the number of multiple of 125.
  • Divide the number by 125 and count the number of multiple of 625 and so on …

Program for Counting Factorial Trailing Zeroes in java

That’s all about counting Factorial Trailing Zeroes in java.
For more programs, refer to data structure and algorithms interview questions in java.

Related Posts

  • Data structures in java
    16 April

    Data Structures in java

    In this post, we will see about various data structures in java. Data structure is a way of storing and organizing data. Data structure provide a way to process and store data efficiently. For example: 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to […]

  • 29 November

    Top 100+ Java coding interview questions

    I have been posting data structure and coding interview questions on various topics such as Array, Queue, Stack, Binary tree, LinkedList, String, Number, ArrayList, etc. So I am consolidating a list of java coding interview questions to create an index post. I will keep adding links to this post whenever I will add new java […]

  • 18 April

    Minimum Number of Jumps to reach last Index

    If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see how to find Minimum Number of Jumps to reach last Index. Problem Given an array A of positive integers possibly zeroes, every index indicating the maximum length of a jump that can […]

  • 20 February

    Inorder Successor in a Binary Search Tree

    If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see how to find Inorder Successor in a Binary Search Tree. Problem Given a Binary Search Tree and a target node value. Find the Inorder successor of the given node value in the […]

  • 04 February

    LCA of a K-ary Tree in O(Sqrt(height))

    If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see about how to find Lowest Common Ancestor of a K-ary Tree in O(Sqrt(height)).We have already seen how to find LCA of n-ary tree in O(n) complexity. Problem Given a K-ary tree and […]

  • 03 February

    Largest Rectangular Area in a Histogram

    If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see about how to find largest rectangular area in a Histogram. Problem Given an Integer representing number of bars in a Histogram and an array of integers representing the height of the bars […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.