Java Program to Count Odd Numbers in an Array

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

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

This program allows the user to enter the size and Array elements. Next, this Java program returns the total number of odd numbers within this array using For Loop.

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

	}
}
Java Program to Count Odd Numbers in an Array 1

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 element. Within the Loop, we used the Java If statement to check if the number divisible by 2 is not equal to 0 or not.

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

In this Count Odd Numbers in an Array example, the user inserted values are a[5] = {25, 98, 56, 89, 17}}

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

The value of i is 0, and the condition (i < 5) is True. So, it enters into the If statements inside the loop.

if(a[i] % 2 != 0) => if(a[0] % 2 != 0)
if(25 % 2 != 0) – Condition is True. So, Java compiler Print the Number and increment the Value of oddCount by 1.
oddCount become 1

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

if(a[1] % 2 != 0)
if(98 % 2 != 0) – Condition is False

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

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

This program is the same as the above odd numbers in an array example. But this time, we used While Loop to count odd numbers an array.

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

	}
}

Java Count Odd Array Items using a While Loop output

 Please Enter Number of elements in an array : 10
 Please Enter 10 elements of an Array  : 25 14 89 63 19 10 12 105 112 115

 List of Odd Numbers in this Array are :25 89 63 19 105 115 
 Total Number of Odd Numbers in this Array = 6

Java Program to Count Odd Numbers in an Array using Methods

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

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

public class CountOdd3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int Size, i, 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();
		}   
		
		oddCount = CountOdd(a, Size);		
		System.out.println("\n Total Number of Odd Numbers in this Array = " + oddCount);

	}
	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;
	}
}

Java Count Odd Array numbers using a While Loop output

Please Enter Number of elements in an array : 8
 Please Enter 8 elements of an Array  : 12 15 13 26 19 77 100 47

 List of Odd  Numbers in this Array are :15 13 19 77 47  
 Total Number of Odd Numbers in this Array = 5