C Program to find the count of even and odd elements in the array

In this blog post, we learn how to write a C program to find the count of even and odd elements in the array? So here will write the C program to find the count of even and odd elements in the array. We will also see how to display the count of even and odd elements in the array.

 

So let’s see the logic to count the even and odd numbers in the given array. Suppose arr is an integer array of size N (arr[N] ), the task is to write the C program to find the count of even and odd elements in the array.

Examples,

//Input array
Input: int arr[5] = {0, 8, 4, 5, 6};

Output: 
Number of even elements = 4    
Number of odd elements = 1
 

//Input array
Input: int arr[5] = {11, 32, 13, 52, 62};

Output: 
Number of even elements = 3 
Number of odd elements = 2

 

 

If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting

 

You can check articles,

 

Logic to count of even and odd elements in the array with the mathematical expression:

1. Create two intermediate variables evenNumCount and oddNumCount.

2. Initialize the variables ‘evenNumCount ’ and oddNumCount with 0.

3. To find the count of even and odd numbers, iterate through each element.

4. If the element is divisible by 2 ( a[i] % 2 == 0 ), increase the even count otherwise odd count. Here ‘i’ is the ith index of the array. It would be from 0 to N-1. N is the size of the array.

 

C Program to find the count of even and odd elements in the array using the mathematical expression:

 

#include <stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])



int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };

    // variables to store even or odd count
    int evenNumCount = 0, oddNumCount = 0;

    int i; //for looping

    //calculate array size
    const int N = ARRAY_SIZE(arr);

    // iterate over the arrays
    for( i = 0; i < N; i++)
    {
        // check for even number
        if(arr[i] % 2 == 0)
        {
            evenNumCount++;
        }
        else
        {
            oddNumCount++;
        }
    }

    printf("Even elements = %d\n", evenNumCount);
    printf("Odd elements = %d", oddNumCount);

    return 0;
}

Output:

find even and element count in array using C

 

 

Logic to count of even and odd elements in the array using binary operation:

1. Create two intermediate variables evenNumCount and oddNumCount.

2. Initialize the variables ‘evenNumCount ’ and oddNumCount with 0.

3. To find the count of even and odd numbers, iterate through each element.

4. By doing AND of 1 and that digit ( a[i] & 1  ), if the result comes out to be 0 then the number is even and increases the even count otherwise odd count. Here ‘i’ is the ith index of the array. It would be from 0 to N-1. N is the size of the array.

 

C Program to find the count of even and odd elements in the array using the binary operation:

 

#include <stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])



int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };

    // variables to store even or odd count
    int evenNumCount = 0, oddNumCount = 0;

    int i; //for looping

    //calculate array size
    const int N = ARRAY_SIZE(arr);

    // iterate over the arrays
    for(i = 0; i < N; i++)
    {
        // check for even number
        if ((arr[i] & 1) == 0)
        {
            evenNumCount++;
        }
        else
        {
            oddNumCount++;
        }
    }

    printf("Even elements = %d\n", evenNumCount);
    printf("Odd elements = %d", oddNumCount);

    return 0;
}

Output:

find even and element count in array using C

 

 

Recommended Articles for you: