Print the Elements of an Array Present in Odd Positions in Java


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.

As per the problem statement we have to find the array elements which are present in the odd position in an array and print them.

A number is said to be even if it is divisible by 2 else it is called an odd number.

Note − The array must be an integer array.

Let’s explore the article to see how it can be done by using Java programming language.

To Show You Some Instances

Instance-1

Suppose the original array is {12, 5, 77, 14, 91, 21, 1, 50}

After finding the odd element position in an array, the result will be −

Odd position of elements present in an array are: [5, 14, 21, 50]

Instance-2

Suppose the original array is {12, 23, 11, 64, 5, 87, 22, 67, 100};

After finding the odd element position in an array, the result will be −

Odd position of elements present in an array are: [23, 64, 87, 67]

Instance-3

Suppose the original array is {11, 22, 33, 44, 55}

After finding the odd element position in an array, the result will be −

Odd position of elements present in an array are: [22, 44]

Algorithm

  • Step 1 − Declare and initialize an integer array.

  • Step 2 − Initialize for loop and check for its length.

  • Step 3 − Start the loop from 1 and add 2 to each index of an array till the last index.

  • Step 4 − Print the elements of the array.

Syntax

To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.

Below refers to the syntax of it −

array.length

where, ‘array’ refers to the array reference.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Array

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Array

Example

In this approach, array elements will be initialized in the program. Then as per the algorithm, we have to find the array elements which are present in the odd position in an array and print them.

public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int num[] = {12, 5 , 77, 14, 91, 21, 1};      
      System.out.println("Odd position of elements present in an array are: ");
      
      //logic implementation
      for (int i = 1; i < num.length; i = i+2){  
         System.out.print(num[i]+" ");  
      }  
   }
}

Output

Odd position of elements present in an array are: 
5 14 21

Approach-2: By Using User Defined Method

Example

In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside the method as per the algorithm we have to find the array elements which are present in the odd position in an array and print them.

public class Main{

   //main method
   public static void main(String[] args){
      int num[] = {12, 23, 11, 64, 5, 87, 22, 67, 100};
      
      // calling the user defined method
      odd_elements(num);
   }
   
   //method body
   public static void odd_elements(int []num){
      System.out.println("Odd position of elements present in an array are: ");
      
      //logic implementation
      for (int i = 1; i < num.length; i = i+2){  
         System.out.print(num[i]+" ");  
      }  

   }
}

Output

Odd position of elements present in an array are: 
23 64 87 67

In this article, we explored how to print the elements on an array which are present in odd index positions by using Java programming language.

Updated on: 05-Jan-2023

700 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements