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

In the previous article, we have seen Java Program to Print the Elements of an Array Present in Even Position

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

Java Program to Print the Elements of an Array Present in Odd 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 odd position.

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

Approach:

  • Take an array.
  • Use a for loop which starts from index 1 and 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 odd positions: ");
        for(int i = 1; i < arr.length ; i+=2)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}

Output:

The array elements at odd positions: 
22 22

Method-2: Java Program to Print the Elements of an Array Present in Odd 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 which starts from index 1 and 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 odd positions: ");
        for(int i = 1; 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 odd positions: 
20 40 60

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: