Count even and odd numbers in an array
C program to count even and odd numbers in an array.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
int arr[MAX];
int i, n;
int count_even, count_odd;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&arr[i]);
}
count_even = 0;
count_odd = 0;
for(i=0;i<n;i++)
{
if(arr[i]%2==0)
{
count_even++;
}
else
{
count_odd++;
}
}
printf("\nTotal count of even numbers\t:%d",count_even);
printf("\nTotal count of odd numbers\t:%d",count_odd);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements
Element 1 :1
Element 2 :2
Element 3 :3
Element 4 :43
Element 5 :54
Element 6 :6
Element 7 :7
Element 8 :87
Total count of even numbers :3
Total count of odd numbers :5