C program To Count and Display Even and Odd Elements of An Array


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

count and display even and odd elements of array

Video Tutorial: C program To Count and Display Even and Odd Elements of An Array


[youtube https://www.youtube.com/watch?v=WZtK9PnOxTk]

YouTube Link: https://www.youtube.com/watch?v=WZtK9PnOxTk [Watch the Video In Full Screen.]

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.
scale

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++;
        }
    }
ia[i]a[i] % 2 == 0ResultDisplayCount
0a[0] = 1010 % 2TRUE101
1a[1] = 1111 % 2FALSE
2a[2] = 1212 % 2TRUE122
3a[3] = 1313 % 2FALSE
4a[4] = 1414 % 2TRUE143

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++;
        }
    }
ia[i]a[i] % 2 != 0ResultDisplayCount
0a[0] = 1010 % 2FALSE
1a[1] = 1111 % 2TRUE111
2a[2] = 1212 % 2FALSE
3a[3] = 1313 % 2TRUE132
4a[4] = 1414 % 2FALSE

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

Leave a Reply

Your email address will not be published. Required fields are marked *