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

In the previous article, we have seen Java Program to Right Rotate the Elements of an Array

In this article we are going to print array elements at even position.

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

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to print array elements at even position.

Method-1: Java Program to Print the Elements of an Array Present in Even Position By Static Initialization of Array Elements

Approach:

  • Take an array.
  • Use a for loop that increments by two after each iteration.
  • Print the array elements.

Program:

import java.util.*;
public class Main
{
    public static void main(String args[])
    {
        //Original array
        int arr1[] = {12, 22, 34, 22, 54};
        // Printing even location elements
        printArray(arr1);
        
    }

    // Function to print the array
    static void printArray(int arr[])
    {
        System.out.println("The array elements at even positions: ");
        for(int i = 0; i < arr.length ; i+=2)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

The array elements at even positions: 
12 34 54

Method-2: Java Program to Print the Elements of an Array Present in Even Position By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Use a for loop that increments by two after each iteration.
  • Print the array elements.

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in); 
        System.out.print("Enter the number of elements in the array: "); 
        int num = sc.nextInt(); 
        int arr[] = new int[num]; 
        System.out.print("Enter the elements: "); 
        for (int i = 0; i < num; i++) 
        { 
            arr[i] = sc.nextInt(); 
        }
        // Printing even location elements
        printArray(arr);
        
    }

    // Function to print the array
    static void printArray(int arr[])
    {
        System.out.println("The array elements at even positions: ");
        for(int i = 0; i < arr.length ; i+=2)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

Enter the number of elements in the array: 6
Enter the elements: 10 20 30 40 50 60
The array elements at even positions: 
10 30 50

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: