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..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

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

Result

java techstudy.org

     





Related Topics




Popular Posts