# Java program to count numbers of even and odd elements in an array

## Count even and odd element in Java

Here, in this page we will discuss the program to count even and odd element in java programming language. We are given with an integer array and need to print the count of even and odd elements present in it.

Here, we will discuss the following two methods to print the count of even elements and odd elements.

• Method 1 : Using Modulo operator
• Method 2 : Using Bit-wise AND operator.

Let’s discuss above two methods one by one,

## Method 1 :

• Take two variables, countEven=0, countOdd=0.
• Iterate over the array,
• Increment countEven if(arr[i]%2==0)
• Otherwise increment the value of countOdd by 1.

### Method 1 : Code in Java

Run
```class Main{
public static void main (String[] args)
{
int arr[] = {1, 20, 60, 31, 75, 40, 80};
int n = arr.length;
int countEven = 0, countOdd = 0;

for(int i=0; i<n; i++){
if((arr[i] % 2 )== 0)
countEven += 1;

else
countOdd += 1;
}
System.out.println("Even Elements count : "+ countEven);
System.out.println("Odd Elements count : "+ countOdd);
}
}```

### Output :

`Even Elements count : 4Odd Elements count : 3`

## Method 2 :

In this method we will use bit-wise AND operator. By doing AND of 1 with array element, if the result comes out to be 0 then the number is even otherwise odd.

### Method 2 : Code in Java

Run
```class Main{
public static void main (String[] args)
{
int arr[] = {1, 20, 60, 31, 75, 40, 80};
int n = arr.length;
int countEven = 0, countOdd = 0;

for(int i=0; i<n; i++){
if((arr[i] & 1 )== 0)
countEven += 1;

else
countOdd += 1;
}
System.out.println("Even Elements count : "+ countEven);
System.out.println("Odd Elements count : "+ countOdd);
}
}```

### Output :

`Even Elements count : 4Odd Elements count : 3`

### 6 comments on “Java program to count numbers of even and odd elements in an array”

• Sanjeet

//Try this in C:
#include
int main()
{
int arr[50],n,i,odd=0,even=0;
printf(“Enter the size of array: “);
scanf(“%d”,&n);
printf(“Enter the value in array: \n”);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
if(arr[i]%2==0)
{
even=even+1;
}
else
{
odd=odd+1;
}
}
printf("The number of even element in array is : %d\n",even);
printf("The number of odd element in array is: %d",odd);
return 0;
}

• Ayushi

#Python Program
size = int(input(“Enter array size: “))
arr = []
for i in range(size):
arr.append(int(input()))

count1 = 0
count2 = 0

for i in arr:
if i%2==0:
count1 += 1
else:
count2 += 1
print(arr)
print(“Number of Even elements {}”.format(count1))
print(“Number of Odd elements {}”.format(count2))

• NITIN

In python :-
n = list(map(int,input().split()))
c = 0
d = 0
for i in n:
if i%2 == 0:
c = c+1
else:
d = d+1
print(‘even element’,c)
print(‘odd element’,d)

• Manish

#include
using namespace std;
int main()
{
int size,i,count=0,digit=0;
cout<>size;
cout<<endl;
int arr[size];
cout<<"Enter the element of the array : ";
for(i=0;i>arr[i];
}
cout<<endl;
for(i=0;i<size;i++)
{
if(arr[i]%2==0)
count++;
else
digit++;
}
cout<<"Number of even no="<<count<<endl;
cout<<"Number of odd no="<<digit<<endl;
return 0;
}

• DHANANJAY

—————————————————————
——–CODE IN PYTHON———
N = int(input(“Enter size of an array: “))
print(“Enter elements of an array: “)
All = []
EVEN = []
ODD = []
for i in range(N):
num = int(input())
n = All.append(num)
for i in range(0,len(All)):
if(i%2==0):
EVEN.append(i)
else:
ODD.append(i)
print(“Event Elements: {}”.format(len(EVEN)))
print(“Odd Elements: {}”.format(len(ODD)))

• Rohit

#———Python Program Counting the number of even and odd elements in an array ——–
arr = list(map(int, input().split()))
even_arr = []
odd_arr=[]
count1=0
count2=0
for i in arr:
if i % 2 ==0:
even_arr.append(i)
count1 += 1
else:
odd_arr.append(i)
count2 += 1

print(even_arr)
print(count1)
print()
print(odd_arr)
print(count2)