
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Print Even Positions Elements from an Array in Java
- Python program to print the elements of an array present on odd position
- Delete All Odd Elements from an Array in Java
- Java Program to Print the Elements of an Array
- Python program to print the elements of an array present on even position
- Java program to Print Odd and Even Number from an Array
- Total number of elements present in an array in C#
- Count number of even and odd elements in an array in C++
- Sorting an array including the elements present in the subarrays in JavaScript
- Absolute Difference of even and odd indexed elements in an Array in C++?
- Python program to print the elements of an array in reverse order
- Absolute Difference of even and odd indexed elements in an Array (C++)?
- Find if sum of odd or even array elements are smaller in Java
- How do you print the content of an array in Java?
- Python program to print the duplicate elements of an array
