1

I have a recursive function and I want to count the number of zeros in it, how do I use a constant to count the zero and not allowing to reset.

int countZeros(int num)
{
    int count = 0;

    if (num > 0)
    {
        if (num % 10 == 0)
            count++;

        return(countZeros(num / 10));
    }
    if (num <= 0)
        return count;

}

For my code, my count will reset once my return function is called. Is there any way to prevent this from happening? I have to return the value back to my main function and display from there.

case 9:
    printf("Enter a number: ");
    scanf("%d", &recursion);
    printf("number of zeros = %d",countZeros(recursion));
    break;
2
  • 1
    Use static variable. The content of a static vaRiable set inside a function remains the same after leaving it. Commented Mar 13, 2014 at 5:12
  • What should 0 give as answer? 1? Commented Mar 13, 2014 at 5:22

5 Answers 5

5

Try this code:

int countZeros(int num)
{
    if (num > 0 && num % 10 == 0)
        return(countZeros(num / 10)+1);

    else
        return 0;
}

It will work in the same way, only note that if your num is negative (but still with zeroes, like -100, it will return 0).

In order to work with negative numbers, use this:

int countZeros(int num)
{
    if (num !=0 && num % 10 == 0)
        return (countZeros(num / 10)+1);

    else
        return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

For this, it will return a 0 back to the main function at the end of the code, and it will still display a 0 in the end.
Yeap, I tried and it doesnt work. I am not sure why.
Doesn't work. It only counts the trailing zeros, not all the zeros in the number.
That's what needs to be mentioned in the question. Small change of the terminating condition will solve it.
2

Make your int static:

 static int count = 0;

Here is a sample run.

Pointers will work as well:

#include <stdio.h>

int countZeros(int num, int * count)
{

    if (num > 0)
    {
        if (num % 10 == 0)
            (*count)++;

    return(countZeros(num / 10,count));
    }
    if (num <= 0)
        return *count;

}

int main(void) 
{

    int count = 0;
    printf("Count = %d",countZeros(1000,&count) );
    return 0;
}

4 Comments

other than static variables, is there any other way?
I'd say a static variable isn't the best solution, invoke it twice or from two separate threads and the second run will not give the correct result.
What if we are not able to add additional calling parameters like count? and we will have to work with only num. Is it still possible?
I am not really sure about that.
1

Avoid static variables, they are evil, for multiple reasons...

The only algorithm that works, and doesn't just count the trailing zeros, is ring0's answer, but please, local variables are free, and explicit code helps not only the reader, but is much more maintainable.

Run it here.

#include <stdio.h>

int main(void)
{
    printf("Count = %d\n", countZeros( 10100) );
    printf("Count = %d\n", countZeros(-10010) );
    return 0;
}

int countZeros(int num)
{
    // Final stop
    if (num == 0 )
        return 0;

    // Recursion for numbers to the left
    int count = countZeros(num / 10);

    // Increase count if current unit is 0
    if (num % 10 == 0)
        count++;

    return count;
}

Explanation:

For the recursion, you need a converging process and a stop condition. The first IF is the base case. Dividing 3 (or -3 for that matter) by 10 will always end up being 0. This is what ends the recursion (stop condition).

The second and last blocks are interchangeable. If the rightmost number is 0, you increase the counter, but then, you also need to add the count result from all the numbers to the left. This is done by seeding it only what you didn't count, hence the division by 10 (to converge).

Both division and modulo works the same for negative and positive numbers, so you keep the behavior for both ends of the integer range.

1 Comment

your code works, but is it possible to explain the recursion part. I dont get it. If the function consider a recursive function?
1

Without any more variable

int countZeros(int n) {
    return n ? (n % 10 ? 0:1)+countZeros(n/10) : 0;
}

countZeros works also with negative numbers.

Example

printf("%d\n", count( 10001));  // prints "3"
printf("%d\n", count(-10001));  // prints "3"

Comments

0

use static variable.

static int count = 0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.