Count Digits Question

For this question, can we take the input as a string?

yes , you can ,as long as your logic gets you the right answer

https://ide.codingblocks.com/s/368193 This is my code. However, it fails in 2 test cases. I can’t figure out why.

https://ide.codingblocks.com/s/368198 This one I tried using double. However this one I guess shows a problem when decimal gets stored in approximate values (56.32321 is 56.3231999). Am I correct?

you dont need to complicate this.see this:

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;
}

This one worked! Thanks!

https://ide.codingblocks.com/s/368250 This was what I had tried before, but it did not work( 2 test cases failed). I can’t figure out why.

if the count is 0 ,u need to print 0 not -1
that’ll pass all the testcases .
if this solves your doubt please mark it as resolved :slight_smile:

Oo yes! I guess I assumed it based on the pythagoras triplet question I had solved before it :sweat_smile:… Thank You!

1 Like