Count Even/Odd Digits in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
int main() {
    long n; int even=0, odd=0;
    scanf("%ld", &n);
    while(n>0){
        int d=n%10;
        if(d%2==0) even++; else odd++;
        n/=10;
    }
    printf("Even: %d, Odd: %d", even, odd);
}

C Output

Input:
482159

Output:
Even: 3, Odd: 3


C++ Program

#include <iostream>
using namespace std;
int main() {
    long n; int even=0, odd=0;
    cin >> n;
    while(n>0){
        int d=n%10;
        (d%2==0) ? even++ : odd++;
        n/=10;
    }
    cout << "Even: " << even << ", Odd: " << odd;
}

C++ Output

Input:
7314

Output:
Even: 2, Odd: 2


JAVA Program

import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        int even=0, odd=0;
        while(n>0){
            int d = (int)(n%10);
            if(d%2==0) even++; else odd++;
            n/=10;
        }
        System.out.println("Even: " + even + ", Odd: " + odd);
    }
}

JAVA Output

Input:
26015

Output:
Even: 3, Odd: 2


Python Program

n = int(input())
even = odd = 0
while n > 0:
    d = n % 10
    if d % 2 == 0:
        even += 1
    else:
        odd += 1
    n //= 10
print("Even:", even, ", Odd:", odd)

Python Output

Input:
97532

Output:
Even: 2 , Odd: 3


In-Depth Explanation
Example
Suppose we have the number 482159. We examine each digit:

9 → odd (odd count: 1)

5 → odd (odd count: 2)

1 → odd (odd count: 3)

2 → even (even count: 1)

8 → even (even count: 2)

4 → even (even count: 3)

Final result: Even: 3, Odd: 3

Real-Life Analogy
Picture placing candies into two containers — one container for red candies (odd digits) and the other container for blue candies (even digits). You pick up each candy, and you examine it for color (digit type), then place it into the appropriate container. At the end, you count the number of candies in each container — that's precisely what this program does with digits.

How the Logic Works
We keep repeating:

Get the last digit by using n % 10.

Determine whether the digit is even by using digit % 2 == 0.

Increment the even or odd counter as required.

Delete the last digit by using integer division (n /= 10 or n //= 10 in Python).

Repeat for the number equals zero.

This way, each digit will be tested once, and the time complexity will be O(k) where k is the number of digits.

Why It Matters
Even and odd digit counting is a basic skill for number manipulation exercises. The same reasoning can be applied to:

Digital cryptography analysis (digit parity check)

Bank security verification tests (odd-even account test)

Data sorting in number theory

Interview Use Case
This question will test your:

Modulo operation knowledge

Loop control

Handling conditions
It's a very common warm-up interview question, as it can be quickly adjusted to prime digits count, sum of even/odd digits, or even digit frequency per se.

SEO-Friendly Summary
Even and odd digit counting in a number is an age-old computer programming problem that assists beginners in honing their loops, modulo arithmetic, and condition checking skills. By retrieving the digits one by one and categorizing them, we can effectively distinguish between the even digits and the odd ones. This idea has practical uses in cryptography, banking systems, and number theory studies. Solving this simple but significant problem in an efficient manner lays a solid foundation for more advanced algorithmic problems.