
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Trailing Zeroes in Factorial of a Number in Java
To count trailing zeroes in factorial of a number, the Java code is as follows −
Example
import java.io.*; 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 " + trailing_zero(num)); } }
Output
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.
Advertisements