« Count number of zeros in integer

Problem

Count number of Zeros in integer

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

Solution

1#include <iostream>
2#include <iomanip>
3#include <algorithm>
4#include <string>
5#include <cstring>
6#include <vector>
7#include <cmath>
8#include <map>
9#include <climits>
10// climits for INT_MIN
11#include <unordered_map>
12using namespace std;
13
14int countZeros(int n)
15{
16 if (n / 10 == 0)
17 {
18 if (n % 10 == 0)
19 {
20 return 1;
21 }
22 else
23 {
24 return 0;
25 }
26 }
27 int x = countZeros(n / 10);
28 int r = n % 10;
29 if (r == 0)
30 {
31 return x + 1;
32 }
33 else
34 {
35 return x;
36 }
37}
38
39int main()
40{
41 int n;
42 cin >> n;
43 cout << countZeros(n) << endl;
44 return 0;
45}