In this article, we will show you, How to write a Java Program to Count Even and Odd Numbers in an Array using For Loop, While Loop, and Functions with example.

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

This program allows the user to enter the size, and the One Dimensional Array elements. Next, it will count the total number of even and odd numbers within this array using For Loop.

// Java Program to Count Even and Odd Numbers in an Array using For Loop import java.util.Scanner; public class CountEvenOdd1 { private static Scanner sc; public static void main(String[] args) { int Size, i; int evenCount = 0, oddCount = 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(); } for(i = 0; i < Size; i++) { if(a[i] % 2 == 0) { evenCount++; } else { oddCount++; } } System.out.println("\n Total Number of Even Numbers in this Array = " + evenCount); System.out.println("\n Total Number of Odd Numbers in this Array = " + oddCount); } }

**OUTPUT**

**ANALYSIS**

First, We declared One dimensional array of user specified size. Next, we used For Loop to store the user entered values as array element such as a[0], a[1], a[2], a[3], a[4]

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

In the next line, We have one more for loop to iterate each and every element. Within the Loop we used If statement.

Any number that is divisible by 2 is even number. If statement will check whether the remainder of the current array element divided by 2 is exactly equal to 0 or not.

- If the condition is True then it is an Even number, and the compiler will increment evenCount.
- If the condition is False then it is an Odd number. Compiler will increment oddCount.

for(i = 0; i < Size; i++) { if(a[i] % 2 == 0) { evenCount++; } else { oddCount++; } }

User inserted values are a[5] = {10, 25, 19, 26, 15}}

**First Iteration: **for (i = 0; 0 < 5; 0++)

The value of i will be 0 and the condition (i < 5) is True. So, it will start executing the statements inside the loop until the condition fails.

if(a[i] % 2 == 0) => if(a[0] % 2 == 0)

if(10 % 2 == 0) – Condition is True. So, evenCount will become 1

**Second Iteration: **for (i = 1; 1 < 5; 1++)

The condition (i < 5) is True.

if(a[1] % 2 == 0)

if(25 % 2 == 0) – Condition is False. So, oddCount will become 1

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

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

This program is same as above but this time we used While Loop to count the even and odd numbers an array.

// Java Program to Count Even and Odd Numbers in an Array using While Loop import java.util.Scanner; public class CountEvenOdd2 { private static Scanner sc; public static void main(String[] args) { int Size, i = 0, j = 0; int evenCount = 0, oddCount = 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++; } while(j < Size) { if(a[j] % 2 == 0) { evenCount++; } else { oddCount++; } j++; } System.out.println("\n Total Number of Even Numbers in this Array = " + evenCount); System.out.println("\n Total Number of Odd Numbers in this Array = " + oddCount); } }

**OUTPUT**

## Java Program to Count Even and Odd Numbers in an Array using Methods

This program is same as first example. But this time we created separate method to count even numbers, and another method to count odd numbers.

// Java Program to Count Even and Odd Numbers in an Array using Functions import java.util.Scanner; public class CountEvenOdd3 { private static Scanner sc; public static void main(String[] args) { int Size, i; int evenCount = 0, oddCount = 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); oddCount = CountOdd(a, Size); System.out.println("\n Total Number of Even Numbers in this Array = " + evenCount); System.out.println(" Total Number of Odd Numbers in this Array = " + oddCount); } 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; } public static int CountOdd(int [] a, int Size) { int i, oddCount = 0; System.out.print("\n List of Odd Numbers in this Array are :"); for(i = 0; i < Size; i++) { if(a[i] % 2 != 0) { System.out.print(a[i] + " "); oddCount++; } } return oddCount; } }

**OUTPUT**

Thank you for visiting our Blog