Count the total number of even and odd elements in an array with C.
/*
Input
Input array: 1 2 3 4 5 6 7 8 9
Output
Total even elements: 4
Total odd elements: 5
*/
#include <stdio.h>
int main()
{
int arr[100];
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]);
}
even = 0;
odd = 0;
for (i = 0; i < size; i++)
{
if (arr[i] % 2 == 0)
{
even++;
}
else
{
odd++;
}
}
printf("Total even elements: %d\n", even);
printf("Total odd elements: %d", odd);
return 0;
}
Output
Enter size of the array : 5
Enter 5 elements in array : 1 2 3 4 5
Total even elements : 2
Total odd elements : 3
0 Comments