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
- Step 1: Start.
- Step 2: Set even = 0, odd = 0.
- Step 3: Read n.
- Step 4: Set i = 0.
- Step 5: If i < n, go to step 6; otherwise go to step 11.
- Step 6: Read a[i].
- Step 7: If a[i] % 2 == 0, go to step 8; otherwise go to step 9.
- Step 8: Increase even by 1. Go to step 10.
- Step 9: Increase odd by 1.
- Step 10: Increase i by 1. Go to step 5.
- Step 11: Print even.
- Step 12: Print odd.
- 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)
| Step | Line | Box | even | odd | n | i | a[0] | a[1] | a[2] | a[3] | a[4] | a[5] | Output |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 3 | Start | |||||||||||
| 2 | 4 | even = 0, odd = 0 | 0 | 0 | |||||||||
| 3 | 8 | Read n | 0 | 0 | 6 | ||||||||
| 4 | 11 | i = 0 | 0 | 0 | 6 | 0 | |||||||
| 5 | 11 | i < n? | 0 | 0 | 6 | 0 | |||||||
| 6 | 12 | Read a[i] | 0 | 0 | 6 | 0 | 1 | ||||||
| 7 | 13 | a[i] % 2 == 0? | 0 | 0 | 6 | 0 | 1 | ||||||
| 8 | 16 | odd++ | 0 | 1 | 6 | 0 | 1 | ||||||
| 9 | 11 | i++ | 0 | 1 | 6 | 1 | 1 | ||||||
| 10 | 11 | i < n? | 0 | 1 | 6 | 1 | |||||||
| 11 | 12 | Read a[i] | 0 | 1 | 6 | 1 | 2 | ||||||
| 12 | 13 | a[i] % 2 == 0? | 0 | 1 | 6 | 1 | 2 | ||||||
| 13 | 14 | even++ | 1 | 1 | 6 | 1 | 2 | ||||||
| 14 | 11 | i++ | 1 | 1 | 6 | 2 | 2 | ||||||
| 15 | 11 | i < n? | 1 | 1 | 6 | 2 | |||||||
| 16 | 12 | Read a[i] | 1 | 1 | 6 | 2 | 3 | ||||||
| 17 | 13 | a[i] % 2 == 0? | 1 | 1 | 6 | 2 | 3 | ||||||
| 18 | 16 | odd++ | 1 | 2 | 6 | 2 | 3 | ||||||
| 19 | 11 | i++ | 1 | 2 | 6 | 3 | 3 | ||||||
| 20 | 11 | i < n? | 1 | 2 | 6 | 3 |
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.