Count odd or even numbers in Java


Check the Number even or odd using array ,looping and if condition. To count the even and odd numbers in an array first user is allowed to enter size and elements of one dimensional array using nextInt() method of Scanner class. A number which is divisible by 2 and generates a remainder of 0 is called an even number.A number which is divisible by 2 and generates a remainder of 1 is called an odd number.

Source Code

import java.util.Scanner;
 
public class even_odd {
    //Write a program to count even and odd numbers in an array
    public static void main(String[] args) {
        int e = 0, o = 0;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter The Limit : ");
        int n = in.nextInt();
        int a[] = new int[n];
        for (int i = 0; i < n; i++) {
            System.out.println("Enter a[" + i + "] value : ");
            a[i] = in.nextInt();
        }
        for (int element : a) {
            if (element % 2 == 0) {
                e++;
            } else {
                o++;
            }
        }
        System.out.println("Total Even Nos : " + e);
        System.out.println("Total Odd Nos  : " + o);
    }
}

Output

Enter The Limit :
5
Enter a[0] value :
10
Enter a[1] value :
34
Enter a[2] value :
53
Enter a[3] value :
41
Enter a[4] value :
45
Total Even Nos : 2
Total Odd Nos  : 3
To download raw file Click Here

Basic Programs