BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to print the elements of an array present on odd position

By Chaitanya Singh | Filed Under: Java Examples

Here, we will write a java program to print the elements of an array present on odd position. For example, if an array is {2, 12, 23, 7, 6, 15} then we need to display the elements 2, 23 and 6 as they are present in the array on odd positions.

Steps to find even position elements of an Array

  1. Initialize the array.
  2. Run a loop starting from 0 till the length of the given array. Use +2 in the increment part of the loop, to only traverse the odd positions in the array.
    • Print the current element of the array
  3. End of the program.

Program to display array elements on even positions

Please go through the comments mentioned in the following program to understand why we are starting the loop from 0 and why are using i=i+2 in the increment part of the loop.

public class JavaExample {
  public static void main(String[] args) {

    //Initializing the array
    int [] numbers = new int [] {1, 3, 5, 7, 9, 11, 13};

    System.out.println("Array Elements on odd Positions: ");
    /* Note we are using i = i+2 as we are only traversing odd positions
     * Important point here is that the array indices start with 0, which
     * means the odd positions such as 1st, 3rd and 5th positions are having
     * indices 0, 2, 4 and so on. That's why numbers[0] prints 1st position
     * element of the array.
     */
    for (int i = 0; i < numbers.length; i = i+2) {
      System.out.println(numbers[i]);
    }
  }
}  

Output:

Array Elements on odd Positions: 
1
5
9
13

Related Java Programs

  1. Java Program to print the elements of an array present on even position
  2. Java Program to Sort an array
  3. Java Program to reverse an array
  4. Java Program to calculate average using array
❮ Java TutorialJava Programs ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap