C Program
void countEvenOdd(int n, int* even, int* odd) { *even = *odd = 0; while (n) { if ((n % 10) % 2 == 0) (*even)++; else (*odd)++; n /= 10; } }
C Output
Input: 12345 Even: 2 Odd: 3
C++ Program
void countEvenOdd(int n, int &even, int &odd) { even = odd = 0; while (n) { ((n % 10) % 2 == 0) ? even++ : odd++; n /= 10; } }
C++ Output
Input: 24689 Even: 3 Odd: 2
JAVA Program
void countEvenOdd(int n) { int even = 0, odd = 0; while (n != 0) { if ((n % 10) % 2 == 0) even++; else odd++; n /= 10; } System.out.println("Even: " + even + " Odd: " + odd); }
JAVA Output
Input: 90817 Even: 3 Odd: 2
Python Program
def count_even_odd(n): even = odd = 0 while n: (even, odd) = (even + 1, odd) if n % 10 % 2 == 0 else (even, odd + 1) n //= 10 return even, odd
Python Output
Input: 13579 Even: 0 Odd: 5
In-Depth Explanation
Example
Suppose the given number is 12345. We examine each digit one after another:
5 → odd
4 → even
3 → odd
2 → even
1 → odd
Total: Even = 2, Odd = 3
We remove digits from right to left with n % 10, determine whether it's even or odd with % 2, and then decrease the number by using integer division n // 10 (or n /= 10 in C/C++/Java).
Real-Life Analogy
Imagine every digit as a player in a team. You're dividing players into two teams: even numbered jerseys and odd numbered jerseys. When every player arrives, you look at the last digit of their number, and put them in their respective group. You'll have two sorted teams at the end.
Another real-world example: imagine a locker room where individuals go in depending on their token number being even or odd. Counting who enters which, you have two different counts.
Why It Matters
Learning and distinguishing between even and odd numbers is one of the basic patterns in programming and mathematics. It gets you to learn:
How to pull digits from numbers
Modulus (%) and looping usage
Using conditional logic (if/else) for categorization
It also lays the groundwork for more complex digit-based issues such as verifying alternating even-odd patterns, even/odd symmetry palindromes, or even digital parity checks in hardware systems.
What You Learn from This
From this problem, students learn how to:
Manipulate numbers digit-by-digit
Use integer division and modulus
Apply logical classification inside loops
You also build confidence in decomposing a number into useful components — which is central to a lot of number-based algorithms such as reversing digits, Armstrong numbers, and digit sum problems.
Interview Relevance and Real Projects
This question is often applied in beginner interviews to challenge:
Number manipulation skills
Application of arithmetic operators
Loop and conditional logic operations
In practical situations, even and odd classification appears in:
Data partitioning
Statistical computations
Even-odd rule algorithms in graphics and polygon fillings
Sensor ID or signal verification in embedded systems
SEO-Optimized Explanation
Even and odd digit counting in a number is one of the most traditional programming exercises that educates number extraction, modular arithmetic, and control flow in loops. The principle is to extract each digit from the number, determine whether it is even or odd using the modulo operator, and maintain the counts. This method is extensively applied in numerical analysis, data classification, and digital verification processes. Solving even and odd count problems in C, C++, Java, and Python enhances the confidence of a beginner in digit-level operations and logical statements, creating a solid foundation for advanced-level competitive programming and coding interviews.
Social Plugin