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.
Example
arr[] = [101, 200, 301, 400, 501]Even Elements count = 2 (200, 400)
Odd Elements count = 3 (101, 301, 501)
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.
Modulo Operator
The modulo operator, denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division. Syntax: If x and y are integers, then the expression: x % y. produces the remainder when x is divided by y.So, if a number is even then it return 0 on modulo it by 2, otherwise it return 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 : 4
Odd 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.
Bit-wise AND Operator
The bitwise AND operator ( & ) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1.Otherwise, the corresponding result bit is set to 0. Both operands to the bit-wise AND operator must have integral types.
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 : 4
Odd Elements count : 3
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment
import java.util.*;
public class CountEvenOdd {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println(“enter size of array”);
int n=sc.nextInt();
int arr[]=new int[n];
System.out.println(“enter elements of array”);
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
CountEvenOddElement(arr,n);
}
public static void CountEvenOddElement(int arr[],int size)
{
int even=0,odd=0;
for(int i=0;i<size;i++)
{
if(arr[i]%2==0)
even++;
else
odd++;
}
System.out.println("number of even elements= "+even);
System.out.println("number of odd elements= "+odd);
}
}
//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;
}
#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))
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)
#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;
}
—————————————————————
——–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)))
#———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)