[LeetCode] Count the Digits That Divide a Number

2520. Count the Digits That Divide a Number

Given an integer num, return the number of digits in num that divide num.

An integer val divides nums if nums % val == 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
int countDigits(int num) {
int res = 0;
int n = num;
while(n) {
int d = n % 10;
if(d != 0) {
if(num % d == 0) res += 1;
}
n /= 10;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/01/01/PS/LeetCode/count-the-digits-that-divide-a-number/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.