Syntax error How to find the Odd and Even numbers in an Array in java?

How to find the Odd and Even numbers in an Array in java?



In the loop check, the result of i%2 operation on each element if 0 the element is even else the element is odd.

Example

Live Demo

public class OddNumbersInAnArray {
   public static void main(String args[]) {
      int[] myArray = {23, 93, 56, 92, 39};
      System.out.println("Even numbers in the given array are:: ");
      for (int i=0; i<myArray.length; i++) {
         if(myArray[i]%2 == 0) {
            System.out.println(myArray[i]);
         }
      }
      System.out.println("Odd numbers in the given array are:: ");
      for (int i=0; i<myArray.length; i++) {
         if(myArray[i]%2 != 0) {
            System.out.println(myArray[i]);
         }
      }
   }
}

Output

Even numbers in the given array are::
56
92
Odd numbers in the given array are::
23
93
39
Updated on: 2020-02-19T10:54:29+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements