Java: Find the number of even and odd integers in a given array of integers
27. Count even and odd numbers in array
Write a Java program to find the number of even and odd integers in a given array of integers.
Pictorial Presentation:
Sample Solution:
Java Code:
// Import the java.util package to use utility classes, including Arrays.
import java.util.Arrays;
// Define a class named Exercise27.
public class Exercise27 {
// The main method for executing the program.
public static void main(String[] args) {
// Declare and initialize an array of integers.
int[] array_nums = {5, 7, 2, 4, 9};
// Print the original array.
System.out.println("Original Array: " + Arrays.toString(array_nums));
// Initialize a counter variable for even numbers.
int ctr = 0;
// Use a loop to iterate through the array elements and count even numbers.
for (int i = 0; i < array_nums.length; i++) {
if (array_nums[i] % 2 == 0)
ctr++;
}
// Print the number of even and odd numbers in the array.
System.out.println("Number of even numbers : " + ctr);
System.out.println("Number of odd numbers : " + (array_nums.length - ctr));
}
}
Sample Output:
Original Array: [5, 7, 2, 4, 9] Number of even numbers : 2 Number of odd numbers : 3
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program to count the number of prime numbers in an array.
- Write a Java program to count the number of negative and positive numbers in an array.
- Write a Java program to find the sum of all even and odd numbers in an array separately.
- Write a Java program to count the number of palindrome numbers in an array.
Go to:
PREV : Move all 0s to array end.
NEXT : Difference between max and min values.
Java Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.