Lets write a C program to display even and odd elements of an array, along with their count.
Related Read:
C Program To Count Number of Even, Odd and Zeros In An Array
How To Determine Even and Odd Numbers?
An even number is an integer that is exactly divisible by 2.
Ex: num % 2 == 0
An odd number is an integer that is not exactly divisible by 2.
Ex: num % 2 != 0
Example: Expected Input/Output
Enter 10 integer numbers
1
2
3
4
5
6
7
8
9
10
Even numbers in the array are …
2
4
6
8
10
Odd numbers in the array are …
1
3
5
7
9
Total Even numbers: 5
Total Odd numbers: 5
Visual Representation
Video Tutorial: C program To Count and Display Even and Odd Elements of An Array
[youtube https://www.youtube.com/watch?v=WZtK9PnOxTk]
Source Code: C program To Count and Display Even and Odd Elements of An Array
#include<stdio.h> #define N 10 int main() { int a[N], i, even = 0, odd = 0; printf("Enter %d integer numbers\n", N); for(i = 0; i < N; i++) scanf("%d", &a[i]); printf("\n\nEven numbers in the array are ...\n"); for(i = 0; i < N; i++) { if(a[i] % 2 == 0) { printf("%d\n", a[i]); even++; } } printf("\nOdd numbers in the array are ...\n"); for(i = 0; i < N; i++) { if(a[i] % 2 != 0) { printf("%d\n", a[i]); odd++; } } printf("\n\nTotal Even numbers: %d", even); printf("\nTotal Odd numbers: %d\n", odd); printf("\n"); return 0; }
Output 1:
Enter 10 integer numbers
10
11
12
13
14
15
16
17
18
19
Even numbers in the array are …
10
12
14
16
18
Odd numbers in the array are …
11
13
15
17
19
Total Even numbers: 5
Total Odd numbers: 5
Output 2:
Enter 10 integer numbers
65
12
4
7
87
2
36
45
78
98
Even numbers in the array are …
12
4
2
36
78
98
Odd numbers in the array are …
65
7
87
45
Total Even numbers: 6
Total Odd numbers: 4
Logic To Count and Display Even and Odd Elements of An Array
We ask the user to enter N integer number and store it in array variable a[N]. We iterate or loop through the user input array elements one by one and check if the selected element is perfectly divisible by 2. If true, then its even number, so we display the number/element and keep count of number of even numbers.
Similarly, we loop through the user input array elements one by one once again, and check if the selected element is not perfectly divisible by 2. If true, then its odd number. So we display the odd number and keep the count of number of odd elements.
Once both the iterations are complete, we display the number of even and odd elements count which we kept track of in above two iterations.
Note: We’re considering 0 as even number, as it has odd elements on either side of it. And 0 is perfectly divisible by 2.
Explanation With Example
If a[5] = {10, 11, 12, 13, 14};
For Even Numbers
even = 0; for(i = 0; i < N; i++) { if(a[i] % 2 == 0) { printf("%d\n", a[i]); even++; } }
i | a[i] | a[i] % 2 == 0 | Result | Display | Count |
---|---|---|---|---|---|
0 | a[0] = 10 | 10 % 2 | TRUE | 10 | 1 |
1 | a[1] = 11 | 11 % 2 | FALSE | ||
2 | a[2] = 12 | 12 % 2 | TRUE | 12 | 2 |
3 | a[3] = 13 | 13 % 2 | FALSE | ||
4 | a[4] = 14 | 14 % 2 | TRUE | 14 | 3 |
Even numbers displayed:
10
12
14
Count: 3
For Odd Numbers
odd = 0; for(i = 0; i < N; i++) { if(a[i] % 2 != 0) { printf("%d\n", a[i]); odd++; } }
i | a[i] | a[i] % 2 != 0 | Result | Display | Count |
---|---|---|---|---|---|
0 | a[0] = 10 | 10 % 2 | FALSE | ||
1 | a[1] = 11 | 11 % 2 | TRUE | 11 | 1 |
2 | a[2] = 12 | 12 % 2 | FALSE | ||
3 | a[3] = 13 | 13 % 2 | TRUE | 13 | 2 |
4 | a[4] = 14 | 14 % 2 | FALSE |
Odd numbers displayed:
11
13
Count: 2
For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert