C Program to count even and odd numbers in an array
Learn how to count even and odd numbers in a C array using the modulo operator, with validated code, a dry run, edge cases, and complexity analysis.
An integer is even when it is exactly divisible by 2; otherwise, it is odd. In C, the modulo operator (%) gives the remainder after division, so we can use number % 2 to classify every element in an array.
- If
number % 2 == 0, the number is even. - If
number % 2 != 0, the number is odd.
For example, the array 2, -3, 0, 7, 8, 10 contains four even numbers (2, 0, 8, and 10) and two odd numbers (-3 and 7).
C Program to Count Even and Odd Numbers in an Array
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
int evenCount = 0;
int oddCount = 0;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
}
for (int index = 0; index < size; index++) {
if (array[index] % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
}
printf("Number of even elements = %d\n", evenCount);
printf("Number of odd elements = %d\n", oddCount);
return 0;
}
Sample Output
Enter the number of elements: 6
Enter 6 elements:
2 -3 0 7 8 10
Number of even elements = 4
Number of odd elements = 2
The input values may be entered on one line or on separate lines because scanf() treats whitespace as a separator when reading integers.
How the Program Works
evenCountandoddCountare initialized to0before any elements are examined.- The first loop reads and stores each array element.
- The second loop visits every stored element.
array[index] % 2calculates the remainder after dividing the current element by2.- A remainder of
0increasesevenCount; any nonzero remainder increasesoddCount. - After the traversal, the program prints both counters.
Here is a dry run for the sample array:
| Element | element % 2 | Classification | Even count | Odd count |
|---|---|---|---|---|
| 2 | 0 | Even | 1 | 0 |
| -3 | -1 | Odd | 1 | 1 |
| 0 | 0 | Even | 2 | 1 |
| 7 | 1 | Odd | 2 | 2 |
| 8 | 0 | Even | 3 | 2 |
| 10 | 0 | Even | 4 | 2 |
At the end, evenCount + oddCount equals size. This is a useful check because every integer must be either even or odd.
Is Zero Even or Odd?
Zero is even because it is divisible by 2 without a remainder:
0 % 2 = 0
Therefore, the program correctly adds zero to evenCount.
Does the Program Work with Negative Numbers?
Yes. Negative integers follow the same parity rule as positive integers. In C, the remainder of a negative odd number may be -1, but it is still nonzero.
-8 % 2 = 0 → even
-3 % 2 = -1 → odd
Checking number % 2 == 0 for even numbers is reliable for positive numbers, negative numbers, and zero.
Can Counting Be Done While Reading the Array?
Yes. The modulo check can be placed inside the input loop immediately after each value is stored. That avoids a separate traversal, although both versions still have O(n) time complexity.
Using two loops, as in the complete program, keeps input and processing separate and makes the logic easier for beginners to follow. It also leaves the array available for other operations later in the program.
Time and Space Complexity
- Time complexity:
O(n), because each of thenelements is classified once. Reading the input is alsoO(n), so the total remainsO(n). - Extra space complexity:
O(1)for the counting operation because it uses only two counters and a loop index. The input array itself requiresO(n)storage.
Common Mistakes
- Forgetting to initialize both counters to
0. - Writing
number / 2 == 0instead of using the modulo operator%. - Treating zero as odd even though
0 % 2is0. - Checking only
number % 2 == 1for odd numbers. This can fail for negative odd values because their remainder may be-1; use theelsebranch ornumber % 2 != 0. - Using two independent
ifstatements when oneif-elseis clearer, since an integer cannot be both even and odd. - Reading more elements than the array can hold.
By visiting the array once and checking the remainder of each element, the program counts even and odd values efficiently without changing the original array.