Thursday 8 January 2015

Java Program to find all the Even and Odd numbers in an array


This is a Java Program which finds the even and odd numbers in an array.

Here all the elements in the array are checked for even and odd condition , when the condition satisfies it prints out the result.

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class EvenOddArray {
 public static void main(String[] args) {
  int i, n;
  Scanner scan = new Scanner(System.in);
  System.out.print("How many elements ?\t");
  n = scan.nextInt();
  int a[] = new int[n];
  for (i = 0; i < n; i++) {
   System.out.print("Enter number " + (i + 1));
   a[i] = scan.nextInt();
  }
  scan.close();
  System.out.print("\nThe Even Numbers are : \t");
  for (i = 0; i < n; i++) {

   if (a[i] % 2 == 0)
    System.out.print(a[i] + "\t");

  }
  System.out.print("\nThe Odd Numbers are : \t");
  for (i = 0; i < n; i++) {

   if (a[i] % 2 != 0)
    System.out.print(a[i] + "\t");

  }
 }
}

OUTPUT :
Java - Even and Odd numbers in an array

No comments:

Post a Comment