Doubt in count digits solution

public static int countDigits(int n, int item){

    int cnt = 0;
    while(n != 0){

        int rem = n % 10;

        if(rem == item) cnt++;
        n = n/10;

    }

    return cnt;
}

//sir suppose in this solution if user enter n=0 and item=0
then the count should be 1 isnt it?? as we are counting the number of item in n

hello @tejasddongare
yeah u are right.
handle that case separately by putting an if statement.

i,e
if(n==0){
check whether item match or not update cnt accordingly
} else{
same code that u shared above
}
return cnt

yes actually i had submitted the code and it got accepted after that i saw solution code and in that this case wasnt handle so i asked thnxs sir

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.