# Java Program to Count Even Numbers in an Array

Write a Java Program to Count Even Numbers in an Array using For Loop, While Loop and Functions with example.

## Java Program to Count Even Numbers in an Array using For Loop

This Java program allows the user to enter the size and the One Dimensional Array elements. Next, this Java program returns the total number of even numbers (or elements) within this array using For Loop.

```// Java Program to Count Even Numbers in an Array using For Loop
import java.util.Scanner;

public class CountEven1 {
private static Scanner sc;
public static void main(String[] args)
{
int Size, i, evenCount = 0;
sc = new Scanner(System.in);

System.out.print(" Please Enter Number of elements in an array : ");
Size = sc.nextInt();

int [] a = new int[Size];

System.out.print(" Please Enter " + Size + " elements of an Array  : ");
for (i = 0; i < Size; i++)
{
a[i] = sc.nextInt();
}
System.out.print("\n List of Even Numbers in this Array are :");
for(i = 0; i < Size; i++)
{
if(a[i] % 2 == 0)
{
System.out.print(a[i] +" ");
evenCount++;
}
}
System.out.println("\n Total Number of Even Numbers in this Array = " + evenCount);
}
}```

First, We declared an array of user specified size. Next, we used For Loop to store the user entered values as Array element such as a, a, a, a, a

```for (i = 0; i < Size; i++)
{
a[i] = sc.nextInt();
}```

In the next line, we have one more for loop to iterate each element. Within the Java for loop, we used the If statement to check if the number is divisible by 2

```for(i = 0; i < Size; i++)
{
if(a[i] % 2 == 0)
{
System.out.print(a[i] +" ");
evenCount++;
}
}```

The user inserted values in this Java Program to Count Even Numbers in an Array are a = {10, 19, 27, 89, 12}}

First Iteration: for (i = 0; 0 < 5; 0++)
The value of i is 0, and the condition (i < 5) is True. So, it starts executing the Java statements inside the loop until the condition fails.

if(a[i] % 2 == 0) => if(a % 2 == 0)
if(10 % 2 == 0) – Condition is True. So, it prints the Number and increment the Value of evenCount
evenCount become 1

Second Iteration: for (i = 1; 1 < 5; 1++)
The condition (i < 5) is True.

if(a % 2 == 0)
if(19 % 2 == 0) – Condition is False

Do the same for remaining For Loop iterations until the condition (i < 5) fails.

## Java Program to Count Even Numbers in an Array using While Loop

This Java program is the same as above. But in this program, we used Java While Loop to find the total number of even numbers in an array.

```// Java Program to Count Even Numbers in an Array using While Loop
import java.util.Scanner;

public class CountEven2 {
private static Scanner sc;
public static void main(String[] args)
{
int Size, i = 0, j = 0, evenCount = 0;
sc = new Scanner(System.in);

System.out.print(" Please Enter Number of elements in an array : ");
Size = sc.nextInt();

int [] a = new int[Size];

System.out.print(" Please Enter " + Size + " elements of an Array  : ");
while (i < Size)
{
a[i] = sc.nextInt();
i++;
}
System.out.print("\n List of Even Numbers in this Array are :");
while(j < Size)
{
if(a[j] % 2 == 0)
{
System.out.print(a[j] +" ");
evenCount++;
}
j++;
}
System.out.println("\n Total Number of Even Numbers in this Array = " + evenCount);
}
}```

Java Program to Count Even Array Numbers using a While Loop output

`````` Please Enter Number of elements in an array : 8
Please Enter 8 elements of an Array  : 10 15 26 19 27 46 55 31

List of Even Numbers in this Array are :10 26 46
Total Number of Even Numbers in this Array = 3``````

## Java Program to return Even Numbers in an Array using Methods

This Java program is the same as the first example, but we separated the logic to count even numbers using Method.

```// Java Program to Count Even Numbers in an Array using Functions
import java.util.Scanner;

public class CountEven3 {
private static Scanner sc;
public static void main(String[] args)
{
int Size, i, evenCount = 0;
sc = new Scanner(System.in);

System.out.print(" Please Enter Number of elements in an array : ");
Size = sc.nextInt();

int [] a = new int[Size];

System.out.print(" Please Enter " + Size + " elements of an Array  : ");
for (i = 0; i < Size; i++)
{
a[i] = sc.nextInt();
}

evenCount = CountEven(a, Size);
System.out.println("\n Total Number of Even Numbers in this Array = " + evenCount);
}
public static int CountEven(int [] a, int Size)
{
int i, evenCount = 0;
System.out.print("\n List of Even Numbers in this Array are :");
for(i = 0; i < Size; i++)
{
if(a[i] % 2 == 0)
{
System.out.print(a[i] +" ");
evenCount++;
}
}
return evenCount;
}
}```

Java Count Even Array Items using a for Loop and functions output

`````` Please Enter Number of elements in an array : 10
Please Enter 10 elements of an Array  : 12 15 36 89 76 34 125 116 99 1

List of Even Numbers in this Array are :12 36 76 34 116
Total Number of Even Numbers in this Array = 5``````