#Find the number of digits of n that evenly divide n

28 messages · Page 1 of 1 (latest)

nimble lantern
#

`public class Solution {
public static int countDigits(int n){
int r;
int count=0;
int c =n;

    for(int i=0;c>0;i++){
        r = c % 10;
        if( r==0){
            break;
        }
        if(n % r==0){
            count = count + 1;

        }
        c = c/10;

    }
    return count;
    
}

}`

mental roseBOT
#

This post has been reserved for your question.

Hey @nimble lantern! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

nimble lantern
#

I got one test case failing here
I am struggling for cases where n ends in a zero

#

Ik the mistake but idk how to write the code to correct it😢

light comet
#

why are you breaking out of your loop here:

if( r==0){
   break;
}
nimble lantern
#

yeah i changed that now

#

idk how to handle that last digit 0 case

#

earlier I was confused between the break and continue keyword but either wont solve the problem

mental roseBOT
light comet
#
  1. you are using a binary AND (&) instead of the logical AND (&&)
  2. first check if r != 0 logical and is short-circuiting meaning that the second comparison won't be executed if the first one fails.
light comet
nimble lantern
#

ya

light comet
#

do you have the testcase that fails ?

nimble lantern
#

now it works

#

but i still didnt get how

#

like why is the order impt

light comet
#

because java will work from left to right. First check the comparision on the very left, then the second ... and so on until the nth comparison

nimble lantern
#

still didnt get it

#

ultimately it has to check all comparision before going inside the loop

light comet
#

oh sorry, i am not talking about the loop, I am talking about the if. Your first solution didn't work because you didn't use continue; and you weren't dividing the remaing number by 10 when that if executed.
Here is a working version of your first code:

public class Solution {
    public static int countDigits(int n){
        int r;
        int count=0;
        int c =n;

        for(int i=0;c>0;i++){
            r = c % 10;
            if( r==0){
                c = c/10;
                continue;
            }
            if(n % r==0){
                count = count + 1;

            }
            c = c/10;

        }
        return count;
        
    }
}
#

and for clarity I would change the variable names a little bit:

public static int countDigits(int number){
        int digit;
        int dividingDigits = 0;
        int copy  = number;
        while(copy > 0) {
          digit = copy%10;
          if(digit == 0) {
              c /= 10;
              continue;
          }

          if(number % digit == 0) {
            dividingDigits++;
          }
          c /= 10;
          
        }
        return dividingDigits;
    }
nimble lantern
#

thx a lot

light comet
#

you're welcome. Feel free to close this post when you are done.

mental roseBOT