Skip to main content
Back to problems
#2180
Easy Algorithms

Count integers with even digit sum

Math Simulation
69.8% acceptance
Feb 25, 2026
711
43
Given a positive integer num, return the number of positive integers less than or equal to num whose digit sums are even.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn count_even(num: i32) -> i32 {
    (1..=num)
      .filter(|&x| {
        let mut n = x;
        let mut sum = 0;
        while n > 0 {
          sum += n % 10;
          n /= 10;
        }
        sum % 2 == 0
      })
      .count() as i32
  }
}