3099. Harshad Number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public int sumOfTheDigitsOfHarshadNumber(int x) {
int sum = 0, tmp = x;
while (tmp > 0) {
sum += tmp % 10;
tmp /= 10;
}

if (x % sum == 0) {
return sum;
} else {
return -1;
}
}
}

Reference

3099. Harshad Number