DSA
Find the number of digits of ‘n’ that evenly divide ‘n’.
The objective is to calculate the modulus of each digit in the number n, then determine if that digit divides n. Consequently, increase the counter. Keep in mind that the digit may be 0, therefore handle such scenario.
int countDigits(int n){
int count=0;
for(int i=n; i>0; i=i/10)
{
int temp = i%10;
if(temp>0 && n%temp==0)
count++;
}
return count;
}
Comments
Post a Comment