JAVA SP T


Lets dive into snapshot of my programs!💻

7.Program to print the elements of an array present on even position.

💡Code:

  

                public class EvenPositionElements7078 {
                    public static void main(String[] args) {
                        // Example array
                        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
                
                        // Display the original array
                        System.out.println("Original Array:");
                        displayArray(array);
                
                        // Print elements at even positions
                        System.out.println("\nElements at Even Positions:");
                        printEvenPositionElements(array);
                    }
                
                    // Method to print elements at even positions
                    private static void printEvenPositionElements(int[] arr) {
                        for (int i = 1; i < arr.length; i += 2) {
                            System.out.print(arr[i] + " ");
                        }
                        System.out.println();
                    }
                
                    // Method to display the elements of an array
                    private static void displayArray(int[] arr) {
                        for (int value : arr) {
                            System.out.print(value + " ");
                        }
                        System.out.println();
                    }
                }
                
               

📸Output :

output