Count the Even and Odd Digits in a Number
Count how many digits of an integer are even and how many are odd.
Peel off each digit with number % 10 and test its parity with digit % 2. Increment an even counter when the digit is even and an odd counter otherwise, then drop the digit with number / 10. Repeat until the number becomes 0 and report both tallies.
Problem Statement
Given an integer, count how many of its digits are even and how many are odd.
For example 123456 has the even digits 2, 4, 6 and the odd digits 1, 3, 5,
so both counts are 3.
Treat 0 as an even digit, scan the absolute value so negatives behave like
positives, and do not convert the number to a String.
Input: A single integer n.
Output: Two counts: how many digits are even and how many are odd.
Examples
Input: 123456
Output: Even: 3, Odd: 3Even digits 2, 4, 6 (three) and odd digits 1, 3, 5 (three).
Input: 2468
Output: Even: 4, Odd: 0All four digits 2, 4, 6, 8 are even.
Constraints
-2,147,483,648 <= n <= 2,147,483,6470 counts as an even digit; no String conversion
Think Before You Code
Reveal the questions to ask yourself first
- How do you test whether a single digit is even or odd?
- How do you keep two separate running counts in one pass?
- Is 0 counted as even or odd?
- How should the loop behave when the input itself is 0?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Tally parities in a single sweep of the digits.
- Take the absolute value of
n. - If it is 0, that single digit 0 is even, so report even = 1, odd = 0.
- Set
even = 0andodd = 0. - While the value is not 0:
digit = value % 10.- If
digit % 2 == 0incrementeven, else incrementodd. value = value / 10.
- Report both counts.
Dry Run
Walk through the example step by step
Counting even and odd digits of n = 123456:
step | value | digit | even/odd | even | odd
-----+--------+-------+----------+------+----
1 | 123456 | 6 | even | 1 | 0
2 | 12345 | 5 | odd | 1 | 1
3 | 1234 | 4 | even | 2 | 1
4 | 123 | 3 | odd | 2 | 2
5 | 12 | 2 | even | 3 | 2
6 | 1 | 1 | odd | 3 | 3
end | 0 | — | — | 3 | 3
Solution
Reveal the full Java solution
public class CountEvenOddDigits {
public static int[] countEvenOdd(int n) {
long value = Math.abs((long) n);
if (value == 0) {
return new int[]{1, 0}; // the single digit 0 is even
}
int even = 0;
int odd = 0;
while (value != 0) {
int digit = (int) (value % 10);
if (digit % 2 == 0) {
even++;
} else {
odd++;
}
value /= 10;
}
return new int[]{even, odd};
}
public static void main(String[] args) {
int[] r1 = countEvenOdd(123456);
System.out.println("Even: " + r1[0] + ", Odd: " + r1[1]); // Even: 3, Odd: 3
int[] r2 = countEvenOdd(2468);
System.out.println("Even: " + r2[0] + ", Odd: " + r2[1]); // Even: 4, Odd: 0
}
}
One pass with the % 10 / / 10 pair visits every digit, and digit % 2
classifies it: a remainder of 0 means even, otherwise odd. Returning both
counts in a two-element array keeps the single traversal doing double duty.
Because 0 is even by definition, the input 0 is reported as one even digit
rather than being skipped.
O(d) where d is the number of digits (log10 n)Space: O(1)Common Mistakes
- Classifying 0 as odd — it is even.
- Returning 0 even digits for the input 0 because the loop body never runs.
- Looping while (n > 0), which skips negative inputs.
Edge Cases to Test
- n = 0 reports one even digit and zero odd digits.
- A number of all even digits (like 2468) reports 0 odd.
- Negative numbers count the same digits as their positive form.
Interview Follow-Ups
- How would you also compute the sum of just the even digits in the same pass?
- How would you return the difference between the even-digit and odd-digit counts?
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 →