[LeetCode] Count Integers With Even Digit Sum

2180. Count Integers With Even Digit Sum

Given a positive integer num, return the number of positive integers less than or equal to num whose digit sums are even.

The digit sum of a positive integer is the sum of all its digits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
int sum(int n) {
int res = 0;
while(n) {
res += n % 10;
n/=10;
}
return res;
}
public:
int countEven(int num) {
int res = 0;
for(int i = 1; i <= num; i++) {
int s = sum(i);
if(s%2 == 0) res++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/02/20/PS/LeetCode/count-integers-with-even-digit-sum/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.