
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Even Position Elements from an Array 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 even 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.
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 even element position in an array, the result will be ?
Even position of elements present in an array are: [77, 91, 1]
Instance-2
Suppose the original array is {12, 23, 11, 64, 5, 87, 22, 67, 100};
After finding the even element position in an array, the result will be ?
Even position of elements present in an array are: [11, 5, 22, 100]
Instance-3
Suppose the original array is {11, 22, 33, 44, 55}
After finding the even element position in an array, the result will be ?
Even position of elements present in an array are: [33, 55]
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 2 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 even 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("Even position of elements present in an array are: "); //logic implementation for (int i = 2; i < num.length; i = i+2){ System.out.print(num[i]+" "); } } }
Output
Even position of elements present in an array are: 77 91 1
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 even 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 even_elements(num); } //method body public static void even_elements(int []num){ System.out.println("Even position of elements present in an array are: "); //logic implementation for (int i = 2; i < num.length; i = i+2){ System.out.print(num[i]+" "); } } }
Output
Even position of elements present in an array are: 11 5 22 100
In this article, we explored how to print the elements on an array which are present in even index positions by using Java programming language.