CategoryC Program

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

  1. evenCount and oddCount are initialized to 0 before any elements are examined.
  2. The first loop reads and stores each array element.
  3. The second loop visits every stored element.
  4. array[index] % 2 calculates the remainder after dividing the current element by 2.
  5. A remainder of 0 increases evenCount; any nonzero remainder increases oddCount.
  6. After the traversal, the program prints both counters.

Here is a dry run for the sample array:

Elementelement % 2ClassificationEven countOdd count
20Even10
-3-1Odd11
00Even21
71Odd22
80Even32
100Even42

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 the n elements is classified once. Reading the input is also O(n), so the total remains O(n).
  • Extra space complexity: O(1) for the counting operation because it uses only two counters and a loop index. The input array itself requires O(n) storage.

Common Mistakes

  • Forgetting to initialize both counters to 0.
  • Writing number / 2 == 0 instead of using the modulo operator %.
  • Treating zero as odd even though 0 % 2 is 0.
  • Checking only number % 2 == 1 for odd numbers. This can fail for negative odd values because their remainder may be -1; use the else branch or number % 2 != 0.
  • Using two independent if statements when one if-else is 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.