Sheet 2 (Functions) - Q4

 avatar
itsLu
c_cpp
a year ago
571 B
10
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