Q:

Java program to print EVEN and ODD elements from an array

belongs to collection: Java Array Programs

0

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 

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Array Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to merge two one dimensional arrays... >>
<< Java program to delete a specific element from a o...