Opening Hours :7AM to 9PM
class EvenPositions { public void printEvenPositions(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i % 2 == 0) { System.out.print(arr[i] + " "); } } } } public class Main { public static void main(String[] args) { EvenPositions ep=new EvenPositions(); int[] arr = {1, 2, 3, 4, 5, 6, 7, 8}; ep.printEvenPositions(arr); } }This program uses a for-loop to iterate through the array and print each element that is present on an even position. It checks the current position in the array by looking at the index (i) of the for-loop. If the index is even (i % 2 == 0), the element at that position is printed. Another approach is using the array slicing
public class Main { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 5, 6, 7, 8}; int[] evenPosArr = Arrays.copyOfRange(arr, 0, arr.length, arr.length/2); for (int i : evenPosArr) { System.out.print(i + " "); } } }In both the examples above, the output will be: