Number of even and odd elements in an array

Write a program to count the number of even and odd elements in an array.
Example:
Input:  arr[]={3,2,4,1,5,6,8}
Output: Even numbers count is 4
Odd numbers count is 3

Approach

Java

#include <stdio.h>

int main()
{
    int arr[] = {3241568};

    int n = sizeof(arr) / sizeof(arr[0]);

    int even = 0odd = 0;
    for (int i = 0i < ni++)
    {
        if (arr[i] % 2 == 0)
        {
            even++;
        }
        else
        {
            odd++;
        }
    }
    printf("Even numbers count is %d\n"even);
    printf("Odd numbers count is %d\n"odd);

    return 0;
}


No comments:

Post a Comment