Count even odd numbers in an array

Write a program to count even and odd numbers in an array.

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

public class MyClass {
    public static void main(String args[]) {
       int [] arr = {1,2,3,4,5,6,7,8,9,10,11};

        Set<Integer> st = new TreeSet<>();
        for(int i=0;i<arr.length;i++){
            st.add(arr[i]);
        }

        Iterator<Integer> item = st.iterator();
        while(item.hasNext()){
            System.out.print(item.next() + " "); 
        }
        int countodd = 0;
        int counteven = 0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]%2==0){
                counteven++;
            }
            else{
                countodd++;
            }
        }
        System.out.println(' ');
        System.out.println(counteven);
        System.out.println(countodd);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *