aboutsummaryrefslogtreecommitdiff
path: root/count_ints_even_sum.c
blob: 237c84096230bc8d4dc64388e0d756c3be6ae9ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 2180. Count Integers With Even Digit Sum
#include <stdio.h>

/*
 * given a positive integer 'num', return the number of positive integersless
 * than or equal to 'num' whose digit sums are event. the digitsum of a positive
 * integer is the sum of all of its digits.
 */

int countEven(int num) {
  int ans = 0;
  for (int i = 1; i <= num; i++) {
    int val = i, d = 0;
    while (val) {
      d += val % 10;
      val /= 10;
    }
    if (d % 2 == 0)
      ans++;
  }
  return ans;
}

int main() {
  printf("%d\n", countEven(4));  // expect: 2
  printf("%d\n", countEven(30)); // expect: 14
}