Write a Java program to count the number of even and odd elements in a given array

Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..

import java.util.*; 
public class Javaexcercise {
 public static void main(String[] args)
 {
    int[] nums = {1,2,3,4,5,6,7,8,9,10};
    int ctreven = 0, ctrodd = 0;
    System.out.println("Original Array: "+Arrays.toString(nums)); 
 
    for(int i = 0; i < nums.length; i++) {
        if(nums[i] % 2 == 0)
        {         
          ctreven++;
        }
        else
           ctrodd++;	
    }                 
    System.out.printf("\nNumber of even elements in the array: %d",ctreven);
    System.out.printf("\nNumber of odd elements in the array: %d",ctrodd);
    System.out.printf("\n");	
  }
}

Result

Write a Java program to count the number of even and odd elements in a given array
Write a Java program to count the number of even and odd elements in a given array

Leave a Comment