-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountZeros.java
More file actions
56 lines (52 loc) · 1.25 KB
/
CountZeros.java
File metadata and controls
56 lines (52 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Count Zeros
// Send Feedback
// Given an integer N, count and return the number of zeros that are present in the given integer using recursion.
// Input Format :
// Integer N
// Output Format :
// Number of zeros in N
// Constraints :
// 0 <= N <= 10^9
// Sample Input 1 :
// 0
// Sample Output 1 :
// 1
// Sample Input 2 :
// 00010204
// Sample Output 2 :
// 2
// Explanation for Sample Output 2 :
// Even though "00010204" has 5 zeros, the output would still be 2 because when you convert it to an integer, it becomes 10204.
// Sample Input 3 :
// 708000
// Sample Output 3 :
// 4
import java.util.Scanner;
public class CountZeros {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter Number : ");
int input = s.nextInt();
int ans = countZeroesRec(input);
System.out.print("Output : " + ans);
s.close();
}
public static int countZeroesRec(int input)
{
int count = 0;
if(input == 0)
{
return 1;
}
else if(input < 10)
{
return 0;
}
int digit = input%10;
if(digit==0)
{
count++;
}
return count + countZeroesRec(input/10);
}
}