Count Even and Odd Numbers in an Array
Problem Statement
Given an array of integers, the task is to count how many elements are even and how many are odd.
An integer is:
- Even if it is divisible by 2
- Odd if it is not divisible by 2
Input and Output Format
Input
arr = [10, 21, 4, 45, 66, 93]
Output
Even = 3
Odd = 3
Approach: Linear Traversal with Condition Check
Algorithm
- Initialize two variables evenCount and oddCount to 0
- Traverse the array
- If element % 2 == 0 → increment evenCount
- Else → increment oddCount
- Print both counts
Pseudocode
evenCount = 0
oddCount = 0
for each element in array:
if element % 2 == 0:
evenCount++
else:
oddCount++
print evenCount, oddCount
Dry Run
Array: [10, 21, 4, 45, 66, 93]
10 → Even → Even = 1
21 → Odd → Odd = 1
4 → Even → Even = 2
45 → Odd → Odd = 2
66 → Even → Even = 3
93 → Odd → Odd = 3

Language-wise Implementation
C
#include
int main() {
int arr[] = {10, 21, 4, 45, 66, 93};
int n = sizeof(arr) / sizeof(arr[0]);
int even = 0, odd = 0;
for(int i = 0; i < n; i++) {
if(arr[i] % 2 == 0)
even++;
else
odd++;
}
printf("Even = %d\nOdd = %d", even, odd);
return 0;
}
Output
Even = 3
Odd = 3
C++
#include
using namespace std;
int main() {
int arr[] = {10, 21, 4, 45, 66, 93};
int n = sizeof(arr) / sizeof(arr[0]);
int even = 0, odd = 0;
for(int i = 0; i < n; i++) {
if(arr[i] % 2 == 0)
even++;
else
odd++;
}
cout << "Even = " << even << "\nOdd = " << odd;
return 0;
}
Output
Even = 3
Odd = 3
Java
public class Main {
public static void main(String[] args) {
int[] arr = {10, 21, 4, 45, 66, 93};
int even = 0, odd = 0;
for(int num : arr) {
if(num % 2 == 0)
even++;
else
odd++;
}
System.out.println("Even = " + even);
System.out.println("Odd = " + odd);
}
}
Output
Even = 3
Odd = 3
Python
arr = [10, 21, 4, 45, 66, 93]
even = 0
odd = 0
for num in arr:
if num % 2 == 0:
even += 1
else:
odd += 1
print("Even =", even)
print("Odd =", odd)
Output
Even = 3
Odd = 3
C#
using System;
class Program {
static void Main() {
int[] arr = {10, 21, 4, 45, 66, 93};
int even = 0, odd = 0;
foreach(int num in arr) {
if(num % 2 == 0)
even++;
else
odd++;
}
Console.WriteLine("Even = " + even);
Console.WriteLine("Odd = " + odd);
}
}
Output
Even = 3
Odd = 3
JavaScript
let arr = [10, 21, 4, 45, 66, 93];
let even = 0, odd = 0;
for (let num of arr) {
if (num % 2 === 0)
even++;
else
odd++;
}
console.log("Even =", even);
console.log("Odd =", odd);
Output
Even = 3
Odd = 3
Time and Space Complexity
| Metric | Value |
|---|---|
| Time Complexity | O(n) |
| Space Complexity | O(1) |
Summary
This problem demonstrates how conditional logic is applied during array traversal. Counting even and odd elements is a simple yet essential classification problem that builds the foundation for filtering, partitioning, and rearranging array elements.
