Programming Examples
Java program to accept n elements in array and Count the even and odd elements
Write a JAVA program which accept n integers from user and count the number of Even and Number of Odd elements in Array.
import java.util.*;
class CountEvenOdd
{
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of Array : ");
int n=sc.nextInt();
int num[]=new int[n];
int even=0,odd=0;
System.out.println("Enter the "+n+" Elements of Array");
for(int i=0;i<n;i++)
{
num[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
if(num[i]%2==0)
{
even++;
}
else
{
odd++;
}
}
System.out.println("No. of Even Elements :"+even);
System.out.println("No. of Odd Elements :"+odd);
}
}
Output
Enter the size of Array :
5
Enter the 5 Elements of Array
3
4
6
3
2
No. of Even Elements :3
No. of Odd Elements :2