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