Java Program to print Odd and Even numbers from an Array

In this article, you will learn how to Print Odd and Even numbers from an Array in Java. We can find odd and even numbers in an array using a java program by getting the remainder of each element and checking if it is divisible by 2 or not. If it is divisible by 2, then it is an even number otherwise it is an odd number.

Algorithm

  1. Declare and Initialize an integer array.
  2. Iterate each element of the array using for loop from 0 to array length -1.
  3. Inside the loop, divide the element by 2 and check the remainder. eg: arr[i] % 2.
  4. If the remainder is 0 then it is an even number otherwise it is an odd number.
  5. Print the Odd and Even numbers based on the if condition result.

Let’s see a few examples below:

Example#1. Print Odd and Even numbers from an Array in Java

package com.javacodepoint.array;

public class FindOddEvenInArray {

	// Main method
	public static void main(String[] args) {

		// Declare and Initialize an integer array
		int arr[] = { 2, 4, 3, 8, 11, 50, 7, 15, 5 };

		// Iterate the array
		for (int i = 0; i < arr.length; i++) {

			// Check whether the No. is Odd or Even
			// if reminder is 0 then, No. is an Even otherwise Odd
			if (arr[i] % 2 == 0) {
				// Even
				System.out.println(arr[i] + " is an Even number.");
			} else {
				// Odd
				System.out.println(arr[i] + " is an Odd number.");
			}
		}

	}

}

OUTPUT:

2 is an Even number.
4 is an Even number.
3 is an Odd number.
8 is an Even number.
11 is an Odd number.
50 is an Even number.
7 is an Odd number.
15 is an Odd number.
5 is an Odd number.

Example#2. Print Odd and Even numbers in Java using Scanner

In this example, we will take an array element input from users using the Scanner class. Below is the java program for it:

package com.javacodepoint.array;

import java.util.Scanner;

public class FindOddEvenInArray2 {

	// Main method
	public static void main(String[] args) {

		// Creating scanner object for reading user inputs
		Scanner sc = new Scanner(System.in);

		// Read the size of the Array
		System.out.println("Enter the size of the Array: ");
		int n = sc.nextInt();

		// Declare the integer Array of given size
		int arr[] = new int[n];

		// Reading the Array values
		System.out.println("Enter " + n + " element(s) of the Array: ");
		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}

		// Iterate the array
		for (int i = 0; i < arr.length; i++) {

			// Check whether the No. is Odd or Even
			// if remainder is 0 then, No. is an Even otherwise Odd
			if (arr[i] % 2 == 0) {
				// Even
				System.out.println(arr[i] + " is an Even number.");
			} else {
				// Odd
				System.out.println(arr[i] + " is an Odd number.");
			}
		}

	}

}

OUTPUT:

Enter the size of the Array:
5
Enter 5 element(s) of the Array:
6
2
3
8
9
6 is an Even number.
2 is an Even number.
3 is an Odd number.
8 is an Even number.
9 is an Odd number.

See also:
Java Program to Find the Largest Number in an Array.
Java Program to Find the Smallest Number in an Array.

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *