C Program To Count Number of Even, Odd and Zeros In An Array


Lets write a C program to count number of even, odd and zeros in an array.

What are Even and Odd Numbers?

An even number is an integer that is exactly divisible by 2. An odd number is an integer that is not exactly divisible by 2.

Related Read:
Even or Odd Number: C Program

Example: Expected Output

Enter 10 integer numbers
10
15
0
-1
5
20
21
55
69
40

Even Numbers: 3
Odd Numbers: 6
Zeros: 1

Visual Representation

count even odd zero in an array

Video Tutorial: C Program To Count Number of Even, Odd and Zeros In An Array


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

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

Source Code: C Program To Count Number of Even, Odd and Zeros In An Array

Method 1

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, odd = 0, even = 0, zero = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < N; i++)
    {
        if(a[i] == 0)
            zero++;
        else if(a[i] % 2 == 0)
            even++;
        else if(a[i] % 2 != 0)
            odd++;
    }

    printf("\nEven Numbers: %d\nOdd Numbers: %d\nZeros: %d\n", even, odd, zero);

    return 0;
}

Output:
Enter 10 integer numbers
9
8
7
6
5
4
3
2
1
0

Even Numbers: 4
Odd Numbers: 5
Zeros: 1

Logic To Find Number of Even, Odd and Zeros In An Array

First we ask the user to enter N integer numbers. After that we iterate through the array elements one by one (using a for loop) and check if the fetched number is 0, if true, we’ll increment the value of zero by one. If the fetched element is not zero, then we check if its perfectly divisible by 2, if so, then its even number orelse its odd number – and we increment the values of variable even and odd accordingly.

Note: Make sure to first check if the fetched number is 0. Only after checking this, go further and check for even or odd conditions.

Method 2

#include<stdio.h>

#define N 10

int main()
{
    int a[N], i, even = 0, odd = 0, zero = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(a[i] == 0)
            zero++;
        else if(a[i] % 2 == 0)
            even++;
        else if(a[i] % 2 != 0)
            odd++;
    }

    printf("\nEven No: %d\nOdd No: %d\nZeros: %d\n", even, odd, zero);

    return 0;
}

Output:
Enter 10 integer numbers
0
1
2
3
4
5
6
7
8
9

Even Numbers: 4
Odd Numbers: 5
Zeros: 1

In above source code, once the user inputs a number, we check if the input number is equal to zero or is perfectly divisible by 2 or it is not perfectly divisible by 2. Based on that we increment the values of variable zero, even and odd accordingly.

This is the best solution for this problem statement, as we only write for loop once and we calculate the result as and when user inputs array elements. So less overhead and more efficient.

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 *