Skip to main content

Find the trailing zeroes in factorial of a given number

Ex:- 5 ! = 120 here, the trailing zero is only 1.

       10 ! = 3628800 here, trailing zeroes are 2. 

Java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 import java.util.Scanner;
 public class FactTrailZero{
     public static void main(String []args){
        Scanner sc = new Scanner(System.in);
        System.out.println("enter a number:");
        int num = sc.nextInt();
        int ans = 0;
        while(num!=0){
            ans += (num / 5);
            num = num / 10;
        }
        System.out.println("Trailing zeroes:"+ans);
     }
 }

Python

num = int(input("Enter a number:"))
ans = 0
while num!=0:
    ans += (num//5)
    num = num//10
print("Trailing zeroes:",ans)