Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Print the Elements of an Array Present on Even Position

  • Last Updated : 24 Nov, 2020
Geek Week

The task is to print all the elements that are present in even position. Consider an example, we have an array of length 6, and we need to display all the elements that are present in 2,4 and 6 positions i.e; at indices 1, 3, 5. 


Input: [1,2,3,4,5,6]

Output: 2

Input: [1,2]

Output: 2 

1) First take a class with the name DisplayElementAtEvenPosition.
2) Then inside the main function, declare and initialize an array with values mentioned in the above example.
3) Now in -order to display the values at even position we need to iterate through the array.
4) For this purpose take a for-loop and iterate through the array while incrementing the value with 2 as we need the values at even position.
5) Once done with the approach, compile it to get the output.



// Java Program to Print the Elements of an Array Present on
// Even Position
import java.util.*;
public class EvenPosition {
    public static void main(String[] args)
        // declaration and intilialization of array.
        int[] arr = new int[] { 1, 2, 3, 4, 5, 6 };
        // iterating through the array using for loop
        for (int i = 1; i < arr.length; i = i + 2) {
            // print element to the console


Time Complexity: O(n) where n is the size of an array

Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :