Answers for "write a java program to count even and odd numbers in an array"

1

Java program to print odd and even numbers in an array

import java.util.Scanner;
public class EvenOddArray
{
    public static void main(String[] args)
    {
        int numbers;
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter elements in array : ");
        numbers = sc.nextInt();
        int[] arrNum = new int[numbers];
        System.out.println("Enter " + numbers + " elements : ");
        for(int a = 0; a < numbers; a++) 
        {
            arrNum[a] = sc.nextInt();
        }
        // print odd numbers
        System.out.print("Odd numbers : ");
        for(int a = 0 ; a < numbers ; a++)
        {
            if(arrNum[a] % 2 != 0)
            {
                System.out.print(arrNum[a] + " ");
            }
        }
        System.out.println("");
        // print even numbers
        System.out.print("Even numbers : ");
        for(int a = 0 ; a < numbers ; a++)
        {
            if(arrNum[a] % 2 == 0)
            {
                System.out.print(arrNum[a] + " ");
            }
        }
        sc.close();
    }
}
Posted by: Guest on February-09-2021
1

count number of even and odd elements in an array in java

// count number of even and odd elements in an array in java
import java.util.Scanner;
public class CountEvenOddNumbersInArray
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      int arrSize, a, countEven = 0, countOdd = 0;
      System.out.println("Please enter number of elements of an array: ");
      arrSize = sc.nextInt();
      int[] arrNumbers = new int[arrSize];
      System.out.println("Please enter " + arrSize + " elements of an array: ");
      for(a = 0; a < arrSize; a++)
      {
         arrNumbers[a] = sc.nextInt();
      }
      for(a = 0; a < arrNumbers.length; a++)
      {
         if(arrNumbers[a] % 2 == 0)
         {
            countEven++;
         }
         else
         {
            countOdd++;
         }
      }
      System.out.println("Total number of even numbers in an array java: " + countEven);
      System.out.println("Total number of odd numbers in an array java: " + countOdd);
      sc.close();
   }
}
Posted by: Guest on February-22-2021

Code answers related to "write a java program to count even and odd numbers in an array"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language