Google News
logo
Java Program to print the Elements of an Array present on Even Position
In the following example of Java program that prints the elements of an array present on even position :
Program :
public class PrintEvenPositionElements {

    public static void main(String[] args) {
        
        // Initialize the array
        int[] arr = {10, 20, 30, 40, 50, 60, 70, 80};
        
        // Iterate over the array and print the elements present on even positions
        System.out.print("Elements on even positions: ");
        for(int i = 1; i < arr.length; i += 2) {
            System.out.print(arr[i] + " ");
        }
    }
}
Output :
Elements on even positions: 
20 40 60 80 
Here, we have initialized an array of integers arr with some values. We have then used a for loop to iterate over the array and print the elements present on even positions.

The loop starts from index 1, which is the second position, and increments by 2 in each iteration to move to the next even position. The elements are printed using the System.out.print() statement.