Counting the Number of Even and Odd Elements in an Array in c

Understanding Even and Odd Count

This task involves counting the number of even and odd elements in an array.

We will explore three different methods to achieve this in C.

Method 1: Using Loop

This method iterates through the array and counts even and odd elements.

#include <stdio.h>

void countEvenOdd(int arr[], int n) {
    int evenCount = 0, oddCount = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] % 2 == 0)
            evenCount++;
        else
            oddCount++;
    }
    printf("Even Count: %d, Odd Count: %d\n", evenCount, oddCount);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    countEvenOdd(arr, n);
    return 0;
}
            
Output:
Even Count: 4, Odd Count: 4

Method 2: Using Recursion

This method counts even and odd numbers using recursion.

#include <stdio.h>

void countEvenOddRec(int arr[], int n, int index, int *evenCount, int *oddCount) {
    if (index == n) return;
    if (arr[index] % 2 == 0)
        (*evenCount)++;
    else
        (*oddCount)++;
    countEvenOddRec(arr, n, index + 1, evenCount, oddCount);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    int evenCount = 0, oddCount = 0;
    countEvenOddRec(arr, n, 0, &evenCount, &oddCount);
    printf("Even Count: %d, Odd Count: %d\n", evenCount, oddCount);
    return 0;
}
            
Output:
Even Count: 4, Odd Count: 4

Method 3: Using Bitwise Operations

This method uses bitwise operations to determine even and odd numbers.

#include <stdio.h>

void countEvenOddBitwise(int arr[], int n) {
    int evenCount = 0, oddCount = 0;
    for (int i = 0; i < n; i++) {
        if ((arr[i] & 1) == 0)
            evenCount++;
        else
            oddCount++;
    }
    printf("Even Count: %d, Odd Count: %d\n", evenCount, oddCount);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    countEvenOddBitwise(arr, n);
    return 0;
}
            
Output:
Even Count: 4, Odd Count: 4
Top 100 Codes By Learn-for-free
Start Preparing Arraysform here👇

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Find Largest Element in an Array: C C++ Java Python

Find Smallest Element in an Array: C C++ Java Python

Find the Smallest and Largest Element in an Array: C C++ Java Python

Find Second Smallest Element in an Array: C C++ Java Python

Calculate the Sum of Elements in an Array: C C++ Java Python

Reverse an Array: C C++ Java Python

Sort First Half in Ascending Order and Second Half in Descending: C C++ Java Python

Finding the Frequency of Elements in an Array: C C++ Java Python

Counting the Number of Even and Odd Elements in an Array: C C++ Java Python

Finding Maximum Product Sub-array in a Given Array: C C++ Java Python

Finding Arrays are Disjoint or Not: C C++ Java Python

Finding Equilibrium Index of an Array: C C++ Java Python

Rotation of Elements of Array - Left and Right: C C++ Java Python

Balanced Parenthesis Problem: C C++ Java Python