'Writing A Recursive Function That Counts Zeros

It is possible to count the number of zeros in an integer through a recursive method that takes a single int parameter and returns the number of zeros the parameter has.

So:

zeroCount(1000)

Would Return:

3

You can remove the last digit from an integer by doing: "12345 / 10" = 1234

You can get the last digit from an integer by doing: "12345 % 10" = 5

This is what I have so far:

public static int zeroCount(int num)
{
    if(num % 10 == 0)
        return num;
    else
        return zeroCount(num / 10);
}

Does anyone have any suggestions or ideas for helping me solve this function?



Solution 1:[1]

Run through your code in your head:

zeroCount(1000)

1000 % 10 == 0, so you're going to return 1000. That doesn't make sense.


Just pop off each digit and repeat:

It sounds like homework, so I'll leave the actual code to you, but it can be done as:

zeroes(0) = 1
zeroes(x) = ((x % 10 == 0) ? 1 : 0) + zeroes(x / 10)

Note that without the terminating condition, it can recurse forever.

Solution 2:[2]

public static int zeroCount(int num)
{
    if(num == 0)
       return 0;

    if(num %10 ==0)
        return 1 + zeroCount(num / 10);
    else
        return zeroCount(num/10); 
}

this would work

Solution 3:[3]

There are three conditions here:
1. If number is single digit and 0 , then return 1
2. If number is less than 10 i.e. it is a number 1,2,3...9 then return 0
3. call recursion for zeros(number/10) + zeros(n%10)

zeros(number){
  if(number == 0 ) //return 1
  if(number < 10) //return 0
  else
       zeros(number/10) + zeros(number%10)
}

n/10 will give us the n-1 digits from left and n%10 gets us the single digit. Hope this helps!

Solution 4:[4]

Check this out for positive integers:

 public static int zeroCount(int number) {
    if (number == 0) {
      return 1;
    } else if (number <= 9) {
      return 0;
    } else {
      return ((number % 10 == 0) ? 1 : 0) + zeroCount(number / 10);
    }
  }

Solution 5:[5]

it is a simple problem and you don't need to go for recursion I think a better way would be converting the integer to a string and check for char '0'

public static int zeroCount(int num)
{
String s=Integer.toString(num);
int count=0;
int i=0;
for(i=0;i<s.length;i++)
{
if(s.charAt(i)=='0')
{
count++;
}
}
return count;
}

Solution 6:[6]

You have to invoke your recursive function from both if and else. Also, you were missing a Base Case: -

public static int zeroCount(int num)
{
    if(num % 10 == 0)
        return 1 + zeroCount(num / 10);
    else if (num / 10 == 0)
        return 0;
    else
        return zeroCount(num / 10);
}

Solution 7:[7]

import java.util.*;
public class Count
{
static int count=0;
static int zeroCount(int num)
{
  if (num == 0){
     return 1;
  }
  else if(Math.abs(num) <= 9)
  { 
     return 0;
  } 
  else
  {
     if (num % 10 == 0)
     { // if the num last digit is zero
        count++;
       zeroCount(num/10);
     } // count the zero, take num last digit out
     else if (num%10 !=0){
         zeroCount(num/10);
     }
  }
  return count;
  }

    public static void main(String[] args)
 {
  Scanner sc = new Scanner(System.in);
  System.out.print("Input: ");
  int num = sc.nextInt();
  System.out.println("Output: " +zeroCount(num));
  }  
  }

Solution 8:[8]

public static int count_zeros(int n)
{
    if(n<=9)
    {
        if(n==0)
        {  
            return 1;
        }
        else
        {
            return 0;
        }
    }

    int s=n%10;

    int count=0;

    if(s==0)
    {
        count=1;
    }

    return count+count_zeros(n/10);
}

Solution 9:[9]

 int countZeros(int n){
     //We are taking care of base case 
if(n<=9){         
    if(n==0){
         return 1;
    }
 else
 {
     return 0;
 } 
}     
   int last=n%10;  //last element of number for e.g- 20403, then last will give 3
   int count=0;    //Initalsizing count as zero
   if(last==0){    //We are checking either the last digit is zero or not if it will 
        will update count from 0 to 1
     count=1;
  }
    return count+countZeros(n/10);  //Recursive call 
   } 

Solution 10:[10]

int check(int n){
    if(n==0)
        return 1;
    return 0;

}


int fun(int n)
{
    if(n/10==0)
    {
        if(n==0){
            return 1;
        }
        else{
                return 0;
    }
    }
    return check(n%10)+fun(n/10);

}

Solution 11:[11]

Check this out, this is the solution I came up with.

int countZeros(int input){
//base case
if(input == 0){
    return 1;
}

int count = 0;
int lastDigit = input%10;
if(lastDigit == 0){
  count = 1;
}

//calc the smallInput for recursion
int smallInput = input/10;
//set smallAns = 0, if i/p itself is not 0 and no 0 is present then return smallAns = 0
int smallAns = 0;
//recursion call
if(smallInput != 0){
    smallAns = countZerosRec(smallInput);            
}

//if we get lastDigit = 0 then return smallAns + 1 or smallAns + count, else return smallAns  
if(lastDigit == 0){
    return smallAns+count;
}
else{
    return smallAns;
}}

Solution 12:[12]

You know that x % 10 gives you the last digit of x, so you can use that to identify the zeros. Furthermore, after checking if a particular digit is zero you want to take that digit out, how? divide by 10.

public static int zeroCount(int num)
{
  if(num == 0) return 1;      
  else if(Math.abs(num) < 9)  return 0;
  else return (num % 10 == 0) ? 1 + zeroCount(num/10) : zeroCount(num/10);
}

I use math.Abs to allow negative numbers, you have to import java.lang.Math;

Solution 13:[13]

static int cnt=0;
    public static int countZerosRec(int input) {
        // Write your code here
        if (input == 0) {
            return 1;
        }      
        if (input % 10 == 0) {
            cnt++;           
        }
        countZerosRec(input / 10);                  
        return  cnt;
    }

Solution 14:[14]

CPP Code using recursion:

int final=0;
int countZeros(int n)
{
    if(n==0) //base case
    return 1;
    int firstn=n/10;
    int last=n%10;
    int smallop=countZeros(firstn);
    if(last==0)
        final=smallop+1;
    return final;
}

Solution 15:[15]

int countZeros(int n) 
{
if(n==0)
{
    return 1;
}
if(n<10) // Needs to be java.lang.Math.abs(n)<10 instead of n<10 to support negative int values
{
    return 0;
}
int ans = countZeros(n/10);
if(n%10 == 0)
{
    ans++;
} 
return ans;
}

Solution 16:[16]

public static int countZerosRec(int input){ // Write your code here

    if(input == 0)
        return 1;
    
    if(input <= 9)
        return 0;
    
    if(input%10 == 0)
        return 1 + countZerosRec(input/10);
    
    return countZerosRec(input/10);
}