# Program to count the number of even and odd elements in an array | faceprep

Program to count the number of even and odd elements in an array is discussed here. Given an array of integers, count the total number of odd elements and even elements in the array and display them as output.

##### Input format:
• Input consists of 1 integer and 1 array.
• Integer corresponds to the size of an array.
##### Output format:

output consists of integers

##### Sample Input :

3  (number of elements of the array)
1  (array elements)
2
3

Odd: 2
Even: 1

## Algorithm to count the number of even and odd elements in an array

• Input the number of elements of the array.
• Input the array elements.
• Initialize count_odd = count_even = 0.
• Traverse the array and increment count_odd if the array element is odd, else increment count_even.
• Print count_odd and count_even.

Program to count the number of even and odd elements in an array are given below.

/* C program to count the number of even and odd elements in an array */
#include
int main()
{
int n;
scanf(“%d”,&n);
int arr[n];
for(int i = 0; i < n; i++)
{
scanf(“%d”,&arr[i]);
}
int count_odd =0, count_even = 0;
for(int i = 0; i < n; i++)
{
if(arr[i] % 2 == 1)
count_odd++;
else
count_even++;
}
printf(“Odd: %d”,count_odd);
printf(“\nEven: %d”,count_even);
return 0;
}

#include<stdio.h>
#include<stdlib.h>
int countEven(int n, int *a)
{
int Even=0;
for(int i=0;i<n;i++)
{
if(*(a+i)%2==0)
{
Even++;
}
}
return Even;
}
int countOdd(int n,int*a)
{
int Odd=0;
for(int i=0;i<n;i++)
{
if(*(a+i)%2!=0)
{
Odd++;
}
}return Odd;
}
int main()
{
int *a,n,Even=0,Odd=0;
scanf(“%d”,&n);
a=(int*)malloc(n*sizeof(int));
for(int i=0;i<n;i++)
{
scanf(“%d”,(a+i));
}
printf(“Odd:%d\n”,countOdd(n,a));
printf(“Even:%d”,countEven(n,a));

return 0;
}

/* C++ program to count the number of even and odd elements in an array */
#include
using namespace std;

int main()
{
int n;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
int count_odd =0, count_even = 0;
for(int i = 0; i < n; i++)
{
if(arr[i] % 2 == 1)
count_odd++;
else
count_even++;
}
cout << “Odd: ” << count_odd;
cout << “\nEven: ” << count_even;
return 0;
}

/* Java program to count the number of even and odd elements in an array */

import java.util.*;
public class Main
{
public static void main(String[] args)
{
int n;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int[]arr = new int[n];
for(int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
int count_odd =0, count_even = 0;
for(int i = 0; i < n; i++)
{
if(arr[i] % 2 == 1)
count_odd++;
else
count_even++;
}
System.out.println(“Odd: ” + count_odd);
System.out.println(“Even: ” + count_even);

}
}

# Python program to count the number of even and odd elements in an array
n = int(input())
arr = []
for i in range(0,n):
temp = int(input())
arr.append(temp)
count_odd = 0
count_even = 0
for i in range(0,n):
if(arr[i] % 2 == 1):
count_odd = count_odd + 1
else:
count_even = count_even + 1
print(“Odd:”, count_odd)
print(“Even:”, count_even)

Output: 