Sheet 2 (Functions) - Q4
itsLu
c_cpp
2 years ago
571 B
18
Indexable
/* D. Write a program to count and print the number of zeros in an
integer number which will be entered by the user using a recursive
function.*/
#include <iostream>
using namespace std;
int zeroCounter (int n)
{
if (n < 10)
return 0;
else if (n % 10 == 0)
return zeroCounter(n/10) + 1;
else
return zeroCounter(n/10);
}
int main ()
{
int n, counter;
cout << "Please enter a number to count its zeros: ";
cin >> n;
counter = zeroCounter(n);
cout << "\nThe number of zeros = " << counter;
}
Editor is loading...
Leave a Comment