-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathCount Zeroes
More file actions
42 lines (34 loc) · 737 Bytes
/
Copy pathCount Zeroes
File metadata and controls
42 lines (34 loc) · 737 Bytes
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
Given an integer n, count and return the number of zeros that are present in the given integer using recursion.
#include <iostream>
using namespace std;
int countZeros(int n) {
// Write your code here
if(n==0)
return 0;
int rem=n%10;
if(rem==0)
return 1+countZeros(n/10);
else
return countZeros(n/10);
}
int main() {
int n;
cin >> n;
cout << countZeros(n) << endl;
}
//other approach -
int countZeros(int n) {
// Write your code here
if(n <=0){
return 1;
}
if(n<10) return 0;
//else{
// return 0;
// }
int smallAns = countZeros(n/10);
if(n%10 == 0){
smallAns = smallAns+1;
}
return smallAns;
}