# Leetcode 2520: Count the Digits That Divide a Number Solution

Here, we will see how to solve Count the Digits That Divide a Number Solution of leet code 2520 problem.

You are given an integerÂ num. You have to returnÂ the number of digits inÂ `num`Â that divideÂ `num`.

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

Example 1:

```Input: num = 9
Output: 1
Explanation: 9 divides itself, hence the answer is 1.```

Example 2:

```Input: num = 131
Output: 2
Explanation: 131 is divisible by 1, but not 3. Since 1 occurs twice as a digit, we return 2.```

Example 3:

```Input: n = 1248
Output: 4
Explanation: 1248 is divisible by all of its digits, hence the answer is 4.```

Approach:

• In a loop, get each digit of number
• Check each digit divides the number if `nums % val == 0`.
• If digit divides the number, increment the counter variable by 1

## Count the Digits That Divide a Number Solution in C++ and Go lang:

Here, we will be solving problem in multiple ways with code.

C++ code 1:

```class Solution {
public:
int countDigits(int num) {
int tmp = num, count = 0;

while (tmp) {
int rem = tmp % 10;

if ((num % rem) == 0) {
count++;
}
tmp = tmp / 10;
}
return count;
}
};```

Go code 1:

```func countDigits(num int) int {
tmp := num

var count = 0

for tmp != 0 {
rem := tmp % 10

if (num % rem) == 0 {
count++
}
tmp = tmp / 10
}
return count
}```

Output:

```Input: n = 1248
Output: 4```

Time complexity: O(n)

Space complexity: O(1)

To check more leetcode problem’s solution. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

https://www.techieindoor.com/category/leetcode/

Posted in C++, Easy, golang, golang program, Leetcode