Java Online Compiler
Example: CountEvenOddForLoop in Java
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// CountEvenOddForLoop import java.util.Scanner; // Main class containing the entry point of the program public class Main { public static void main(String[] args) { // Step 1: Declare and initialize the array int[] numbers = {12, 7, 24, 15, 30, 9, 10, 5, 22}; // Step 2: Initialize counters for even and odd numbers int evenCount = 0; int oddCount = 0; // Step 3: Iterate through the array using a traditional for loop for (int i = 0; i < numbers.length; i++) { // Step 4: Check if the current element is even or odd if (numbers[i] % 2 == 0) { evenCount++; // Increment even counter if even } else { oddCount++; // Increment odd counter if odd } } // Step 5: Print the results System.out.println("Array elements: " + java.util.Arrays.toString(numbers)); System.out.println("Number of Even Elements: " + evenCount); System.out.println("Number of Odd Elements: " + oddCount); } }
Output
Clear
ADVERTISEMENTS