×

Java Programs

Java Practice

Java program to print EVEN and ODD elements from an array

In this java program, we are going to learn how to find and print separately EVEN and ODD number from an array? By IncludeHelp Last updated : December 23, 2023

Problem statement

Given a one dimensional array and we have to print its EVEN and ODD elements separately.

Example

Input:
Given array (elements will be read in program): 10 11 12 13 14

Output:
Odd numbers in the array are : 10 12 14
Even numbers in the array are : 11 13 

Program to print EVEN and ODD elements from an array in java

import java.util.Scanner;

public class ExArrayEvenOdd {
  public static void main(String[] args) {
    // initializing and creating object.
    int n;
    Scanner s = new Scanner(System.in);

    // enter number for elements.
    System.out.print("Enter no. of elements you want in array : ");
    n = s.nextInt();
    int a[] = new int[n];

    // enter all elements.
    System.out.println("Enter all the elements : ");
    for (int i = 0; i < n; i++) {
      a[i] = s.nextInt();
    }
    System.out.print("Odd numbers in the array are : ");
    for (int i = 0; i < n; i++) {
      if (a[i] % 2 != 0) {
        System.out.print(a[i] + " ");
      }
    }
    System.out.println("");
    System.out.print("Even numbers in the array are : ");
    for (int i = 0; i < n; i++) {
      if (a[i] % 2 == 0) {
        System.out.print(a[i] + " ");
      }
    }
  }
}

Output

Enter no. of elements you want in array : 15
Enter all the elements : 
5
6
9
3
12
55
44
66
85
95
31
98
74
11
12
Odd numbers in the array are : 5 9 3 55 85 95 31 11 
Even numbers in the array are : 6 12 44 66 98 74 12 

Java Array Programs »

More Java Array Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.