Question 2
Question 2: Count Zero
Statement: You are given a number how many zeroes does N! end on?
Input Format: The first line contains one integer
T i.e number of test cases. The following T lines contain one integer N.
Output Format: For each test case output one integer per line.
Constraint:
T<=1000
0<=N<=1011
Sample Input:
3
9
11
20
Sample Output:
1
2
4
Solution:
C++
Java
Python
C++
Run
#include <bits/stdc++.h>
using namespace std;
int countTrailingZeroes(int N) {
int count = 0;
while (N >= 5) {
N /= 5;
count += N;
}
return count;
}
int main() {
int T;
cin >> T;
while (T--) {
int N;
cin >> N;
cout << countTrailingZeroes(N) << endl;
}
return 0;
}
Java
Run
import java.util.Scanner;
class Main {
public static int countTrailingZeroes(int N) {
int count = 0;
while (N >= 5) {
N /= 5;
count += N;
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while (T-- > 0) {
int N = sc.nextInt();
System.out.println(countTrailingZeroes(N));
}
}
}
Python
Run
def count_trailing_zeroes(N):
count = 0
while N >= 5:
N //= 5
count += N
return count
T = int(input())
for _ in range(T):
N = int(input())
print(count_trailing_zeroes(N))
