Java Program to print Odd and Even Numbers from an Array

We can print odd and even numbers from an array in java by getting remainder of each element and checking if it is divided by 2 or not. If it is divided by 2, it is even number otherwise it is odd number.

  1. public class OddEvenInArrayExample{  
  2. public static void main(String args[]){  
  3. int a[]={1,2,5,6,3,2};  
  4. System.out.println(“Odd Numbers:”);  
  5. for(int i=0;i<a.length;i++){  
  6. if(a[i]%2!=0){  
  7. System.out.println(a[i]);  
  8. }  
  9. }  
  10. System.out.println(“Even Numbers:”);  
  11. for(int i=0;i<a.length;i++){  
  12. if(a[i]%2==0){  
  13. System.out.println(a[i]);  
  14. }  
  15. }  
  16. }}  

Output:

Odd Numbers:
1
5
3
Even Numbers:
2
6
2

Leave a Comment