beginnerDigit ProblemsJava

Find the Sum of the Even Digits in a Number

Sum only the even-valued digits of an integer, using arithmetic only.

Quick Answer

Peel off each digit with number % 10. If the digit is even (digit % 2 == 0), add it to a running total. Drop the digit with number / 10 and repeat until the number is 0. The total is the sum of all even-valued digits.

Problem Statement

Given a non-negative integer, add up only the digits whose value is even (that is, 0, 2, 4, 6, 8) and return the total. Do not convert the number to a String; use arithmetic only.

For example, in 123456 the even digits are 2, 4 and 6, so the sum is 12.

Input: A single non-negative integer n.

Output: The sum of the even-valued digits of n.

Examples

Example 1
Input:  123456
Output: 12

The even digits are 2, 4 and 6; 2 + 4 + 6 = 12.

Example 2
Input:  2468
Output: 20

Every digit is even; 2 + 4 + 6 + 8 = 20.

Constraints

  • 0 <= n <= 2,147,483,647 (fits in a 32-bit int)
  • No String conversion; arithmetic only

Think Before You Code

Reveal the questions to ask yourself first
  • How do you test whether a single digit is even?
  • Which operation gives you one digit at a time from the right?
  • Should the digit 0 contribute anything to the sum?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
A digit is even when `digit % 2 == 0`.
Hint 2
Use `n % 10` to read the last digit and `n / 10` to move to the next.
Hint 3
Add the digit to a running total only when it passes the even test.

Approach

Reveal the step-by-step approach

Walk the number digit by digit, adding only the even ones.

  1. Set sum = 0.
  2. While n is not 0:
    • digit = n % 10.
    • If digit % 2 == 0, add digit to sum.
    • n = n / 10.
  3. Return sum.

The digit 0 is even but adds nothing, so it never changes the result.

Dry Run

Walk through the example step by step

Summing the even digits of n = 123456:

step | n      | digit | even? | sum
-----+--------+-------+-------+----
1    | 123456 | 6     | yes   | 6
2    | 12345  | 5     | no    | 6
3    | 1234   | 4     | yes   | 10
4    | 123    | 3     | no    | 10
5    | 12     | 2     | yes   | 12
6    | 1      | 1     | no    | 12
end  | 0      | —     | —     | 12

Solution

Reveal the full Java solution
public class SumEvenDigits {
    public static int sumEvenDigits(int n) {
        int sum = 0;
        while (n != 0) {
            int digit = n % 10;
            if (digit % 2 == 0) sum += digit;
            n /= 10;
        }
        return sum;
    }

    public static void main(String[] args) {
        System.out.println(sumEvenDigits(123456)); // 12
        System.out.println(sumEvenDigits(2468));   // 20
    }
}

A single pass reads each digit once and the even test decides whether it joins the total. Since 0 is even but contributes nothing, the special case of the input 0 correctly yields a sum of 0 even though the loop body never runs.

Time: O(d) where d is the number of digitsSpace: O(1)

Common Mistakes

  • Confusing even-valued digits with digits at even positions — this problem is about the digit's value.
  • Testing n % 2 (the whole number) instead of digit % 2 (the current digit).

Edge Cases to Test

  • n = 0 returns 0.
  • A number with no even digits, like 13579, returns 0.
  • A number whose digits are all even, like 2468, sums them all.

Interview Follow-Ups

  • How would you compute the sum of the odd digits instead?
  • How would you return both the even-digit sum and the odd-digit sum in one pass?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us