C Program to print only even numbers from an array
Learn how to print only the even numbers stored in a C array using the modulo operator, with validated code, examples, a dry run, and complexity analysis.
An integer is even when it is exactly divisible by 2. To print only the even numbers from an array, visit each element and print it when the remainder after division by 2 is zero.
In C, the condition is:
number % 2 == 0
For example, the even elements in 7, -4, 0, 13, 18, 21 are -4, 0, and 18.
C Program to Print Only Even Numbers from an Array
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
int foundEven = 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;
}
}
printf("Even elements: ");
for (int index = 0; index < size; index++) {
if (array[index] % 2 == 0) {
printf("%d ", array[index]);
foundEven = 1;
}
}
if (!foundEven) {
printf("None");
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 6
Enter 6 elements:
7 -4 0 13 18 21
Even elements: -4 0 18
The values can be entered on one line or on separate lines because scanf() treats whitespace as a separator when reading integers.
How the Program Works
- The program validates the array size so that it remains between
1andMAX_SIZE. - The first loop reads and stores all the array elements.
- The second loop examines one element at a time.
- If
array[index] % 2 == 0, the current element is even and is printed. foundEvenbecomes1whenever an even element is found.- If
foundEvenis still0after the loop, the program printsNoneinstead of leaving the result unexplained.
Here is a dry run for the sample array:
| Element | element % 2 | Is it printed? | Output so far |
|---|---|---|---|
| 7 | 1 | No | — |
| -4 | 0 | Yes | -4 |
| 0 | 0 | Yes | -4 0 |
| 13 | 1 | No | -4 0 |
| 18 | 0 | Yes | -4 0 18 |
| 21 | 1 | No | -4 0 18 |
The program only reads the elements, so their values and positions in the original array remain unchanged.
Why Is Zero Printed?
Zero is an even number because dividing it by 2 leaves no remainder:
0 % 2 = 0
Therefore, the condition correctly prints 0 whenever it appears in the array.
Does It Work with Negative Even Numbers?
Yes. The modulo test works for negative integers as well:
-4 % 2 = 0
-7 % 2 = -1
Because -4 has a remainder of zero, it is even and is printed. The nonzero remainder for -7 means it is odd and is skipped.
What If the Array Has No Even Numbers?
The foundEven flag handles an array containing only odd values. For example:
Enter the number of elements: 4
Enter 4 elements:
3 7 11 15
Even elements: None
Without this flag, the program would print only the label Even elements: and the user might not know whether the program completed correctly.
Printing Versus Counting Even Elements
Printing an even element displays its value immediately. Counting even elements instead requires a counter that is increased whenever the modulo condition is true.
Both operations use the same condition, but they produce different results:
- Printing answers which values are even?
- Counting answers how many values are even?
Time and Space Complexity
- Time complexity:
O(n), because every array element is checked once. Reading the input is alsoO(n), so the complete program remainsO(n). - Extra space complexity:
O(1)for filtering and printing because only a flag and a loop index are required. The input array itself occupiesO(n)space.
Common Mistakes
- Using
/instead of%to test divisibility. - Writing
array[index] % 2 = 0; comparison requires==, while=is assignment. - Treating zero as odd even though it is evenly divisible by
2. - Checking
array[index] % 2 == 1when looking for even values; the even condition must compare with0. - Forgetting to handle the case where no even elements are present.
- Changing array elements while iterating when the task only requires printing them.
A single traversal with the modulo condition is enough to print every even element while preserving the array's original order.