[LeetCode] Check Divisibility by Digit Sum and Product

3622. Check Divisibility by Digit Sum and Product

You are given a positive integer n. Determine whether n is divisible by the sum of the following two values:

  • The digit sum of n (the sum of its digits).
  • The digit product of n (the product of its digits).

Return true if n is divisible by this sum; otherwise, return false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
int sum(int n) {
int res = 0;
while(n) {
res += n % 10;
n /= 10;
}
return res;
}
int mul(int n) {
int res = 1;
while(n) {
res *= n % 10;
n /= 10;
}
return res;
}
public:
bool checkDivisibility(int n) {
return n % (sum(n) + mul(n)) == 0;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/07/20/PS/LeetCode/check-divisibility-by-digit-sum-and-product/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.