#Find the number of digits of n that evenly divide n
28 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @nimble lantern! Please use
/closeor theClose Postbutton 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.
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😢
why are you breaking out of your loop here:
if( r==0){
break;
}
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
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
- you are using a binary AND (
&) instead of the logical AND (&&) - first check if
r != 0logical and is short-circuiting meaning that the second comparison won't be executed if the first one fails.
Do you mean leading 0s ?
ya
I understood ur first point
do you have the testcase that fails ?
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
still didnt get it
ultimately it has to check all comparision before going inside the loop
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;
}
thx a lot
you're welcome. Feel free to close this post when you are done.
Before your post will be closed, would you like to express your gratitude to any of the people who helped you? When you're done, click I'm done here. Close this post!.