C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Count even and odd numbers in array

C Program: Count even and odd numbers in array

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i;

    int evenCount = 0, oddCount = 0;

 

    // Input array size

    printf("Enter number of elements in the array: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    for (i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Count even and odd numbers

    for (i = 0; i < n; i++) {

        if (arr[i] % 2 == 0)

            evenCount++;

        else

            oddCount++;

    }

 

    // Display results

    printf("\nTotal even numbers: %d", evenCount);

    printf("\nTotal odd numbers: %d\n", oddCount);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements in the array: 6
Enter 6 elements:
10 25 30 41 50 77

OUTPUT :
Total even numbers: 3
Total odd numbers: 3

Explanation

  1. The user enters the size and elements of the array.
  2. The program loops through each element:
    • If the element is divisible by 2, it’s even.
    • Otherwise, it’s odd.
  3. The counts of even and odd numbers are displayed at the end.