C program to count even and odd elements in an array


 /*

Alamgir Hossain

Lecturer, Dept. of CSE, Prime University.


C program to count even and odd elements in an array.
Array: 20, 12, 13, 50, 17
Even: 3
Odd: 2
*/
#include<stdio.h>
int main()
{
        int size, arr[100], i, even, odd;
        printf("Enter the size of the array: ");
        scanf("%d", &size);
        printf("Enter the elements of the array: ");
        for(i = 0; i < size; i++){
                scanf("%d", &arr[i]);
        }
        even = 0;
        odd = 0;
        for(i = 0; i < size; i++){
                if(arr[i] % 2 == 0){
                        even = even + 1;
                }
                if(arr[i] % 2 != 0){
                        odd = odd + 1;
                }
        }
        printf("Total even elements: %d\n", even);
        printf("Total odd elements: %d\n", odd);
        return 0;
}


No comments:

Post a Comment