DEV Community

Yesh Rajawat
Yesh Rajawat

Posted on

Recursion | Count the number of Zeros in a number

public class CountZeros {


    public static int countZeros(int n) {
        if(n==0) {
            return 1;
        }
        else {
            return countZero(n);
        }
    }

    public static int countZero(int n) {

        if(n<10) {

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


    }

    public static void main(String ... args) {

        System.out.println(countZeros(110110));

    }

}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)