In this tutorial I’ll show you how to write a c program to input elements in array from user and count even and odd elements in array.
How to find total number of even and odd elements in a given array using loops.
Code: C Program to count even and odd elements in array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** * C program to count total number of even and odd elements in an array */ #include <stdio.h> #define MAX_SIZE 100 //Maximum size of the array int main() { int arr[MAX_SIZE]; int i, size, even, odd; /* Input size of the array */ printf("Enter size of the array: "); scanf("%d", &size); /* Input array elements */ printf("Enter %d elements in array: ", size); for(i=0; i<size; i++) { scanf("%d", &arr[i]); } /* Assuming that there are 0 even and odd elements */ even = 0; odd = 0; for(i=0; i<size; i++) { /* If the current element of array is even then increment even count */ if(arr[i]%2 == 0) { even++; } else { odd++; } } printf("Total even elements: %d\n", even); printf("Total odd elements: %d", odd); return 0; } |
Program Output: