Count Even and Odd Numbers in an Array: flowchart, algorithm and program

This program reads n numbers into an array and prints how many are even and how many are odd. It checks each number with the remainder operator (% 2) and adds one to the matching counter.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Set even = 0, odd = 0.
  3. Step 3: Read n.
  4. Step 4: Set i = 0.
  5. Step 5: If i < n, go to step 6; otherwise go to step 11.
  6. Step 6: Read a[i].
  7. Step 7: If a[i] % 2 == 0, go to step 8; otherwise go to step 9.
  8. Step 8: Increase even by 1. Go to step 10.
  9. Step 9: Increase odd by 1.
  10. Step 10: Increase i by 1. Go to step 5.
  11. Step 11: Print even.
  12. Step 12: Print odd.
  13. Step 13: Stop.

Pseudocode

BEGIN
    even ← 0, odd ← 0
    READ n
    i ← 0
    WHILE i < n DO
        READ a[i]
        IF a[i] % 2 == 0 THEN
            even++
        ELSE
            odd++
        END IF
        i++
    END WHILE
    PRINT even
    PRINT odd
END

Program in C, Python and Java

Count Even and Odd Numbers in an Array program in C

#include <stdio.h>

int main() {
    int n, i, even = 0, odd = 0;
    int a[100];

    printf("Enter n: ");
    scanf("%d", &n);

    printf("Enter %d numbers: ", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        if (a[i] % 2 == 0)
            even++;
        else
            odd++;
    }

    printf("Even numbers: %d\n", even);
    printf("Odd numbers: %d\n", odd);
    return 0;
}

Count Even and Odd Numbers in an Array program in Python

n = int(input("Enter n: "))
a = list(map(int, input("Enter " + str(n) + " numbers: ").split()))

even = 0
odd = 0
for i in range(n):
    if a[i] % 2 == 0:
        even += 1
    else:
        odd += 1

print("Even numbers:", even)
print("Odd numbers:", odd)

Count Even and Odd Numbers in an Array program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int even = 0, odd = 0;

        System.out.print("Enter n: ");
        int n = sc.nextInt();
        int[] a = new int[n];

        System.out.print("Enter " + n + " numbers: ");
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            if (a[i] % 2 == 0)
                even++;
            else
                odd++;
        }

        System.out.println("Even numbers: " + even);
        System.out.println("Odd numbers: " + odd);
    }
}

Sample input: n = 6, numbers = 1, 2, 3, 4, 5, 8
Output: Enter n: Enter 6 numbers: Even numbers: 3 Odd numbers: 3

How it works

  • Start with two counters, even = 0 and odd = 0, before reading any numbers.
  • The for loop runs i from 0 to n-1 and reads one number into a[i] on each pass.
  • For each number, a[i] % 2 == 0 means it divides by 2 with no remainder, so even is increased by 1.
  • If the remainder is not 0, the else branch runs and odd is increased by 1. Every number goes into exactly one counter, so even + odd always equals n.
  • After the loop ends, the program prints the final values of even and odd.
  • Dry run with 1, 2, 3, 4, 5, 8: the odd numbers are 1, 3 and 5, and the even numbers are 2, 4 and 8. That gives even = 3 and odd = 3.

Dry run (trace table)

StepLineBoxevenoddnia[0]a[1]a[2]a[3]a[4]a[5]Output
13Start
24even = 0, odd = 000
38Read n006
411i = 00060
511i < n?0060
612Read a[i]00601
713a[i] % 2 == 0?00601
816odd++01601
911i++01611
1011i < n?0161
1112Read a[i]01612
1213a[i] % 2 == 0?01612
1314even++11612
1411i++11622
1511i < n?1162
1612Read a[i]11623
1713a[i] % 2 == 0?11623
1816odd++12623
1911i++12633
2011i < n?1263

Time and space complexity

Time: O(n) · Space: O(n). The loop runs n times doing constant work each time, and the array stores n numbers.

Common mistakes

  • Forgetting to set even and odd to 0 at the start. Uninitialised variables hold garbage values, so the counts come out wrong.
  • Writing a[i] % 2 = 0 with a single = instead of ==. That is an assignment, not a comparison.
  • Testing only a[i] % 2 == 1 for odd numbers. This fails for negative numbers in C, because -3 % 2 gives -1. Using else, or checking != 0, handles them correctly.
  • Entering n larger than 100 when the array is declared as a[100]. This writes past the end of the array.

Viva questions

Q1. Why are even and odd both initialised to 0 on line 4?

They are counters that are incremented inside the loop, so they must start at 0 to count from nothing. If left uninitialised they would hold garbage values and the final counts would be wrong.

Q2. What does the condition a[i] % 2 == 0 on line 13 check?

The % operator gives the remainder after dividing by 2. If the remainder is 0 the number is divisible by 2, so it is even; otherwise it is odd.

Q3. What happens if the user enters 0 as one of the numbers?

0 % 2 is 0, so 0 is counted as even. Zero is mathematically an even number, so this is correct.

Q4. How does the program handle negative numbers like -3 or -4? Does the check still work?

In C, -3 % 2 gives -1 and -4 % 2 gives 0. Because the code tests == 0 for even and treats everything else as odd, negatives are classified correctly. It would break if we tested == 1 for odd, since -3 % 2 is -1.

Q5. Why is a for loop used here, and how many times does it run?

The number of inputs n is known in advance, so a for loop is the natural choice. It runs exactly n times, with i going from 0 to n-1.

Q6. What is a problem if the user enters n greater than 100?

The array a has size 100, so writing a[i] beyond index 99 overflows the array and causes undefined behaviour. We should check that n is between 1 and 100 before reading, or declare the array according to n.

Q7. What is the time and space complexity of this program?

Time is O(n) because each of the n elements is read and checked once. Space is O(n) for the array, though it could be O(1) if we counted without storing the numbers.

Q8. What would change if you wanted to count without using an array, or count numbers divisible by 3 instead?

Without an array, read each number into a single variable inside the loop, test it, and discard it, since the array is never used again. For divisibility by 3, change the condition to a[i] % 3 == 0 and rename the counters, with the else branch counting the non-multiples.

Frequently asked questions

How do you check if a number is even or odd in C?

Use the remainder operator. If num % 2 == 0 the number is even, and otherwise it is odd.

Do we need an array to count even and odd numbers?

Not really. You can test each number as you read it and update the counters, without storing it. The array is useful if you want to use the numbers again later.

Does this program work for negative numbers and zero?

Yes. Zero is even, so it goes to the even counter. Negative numbers also work because the else branch counts everything that is not even, even though -3 % 2 gives -1 in C.

More programs

← All programs

Try AI Flowchart free →