Java Program to display Array item at Even & Odd position

Program to display Array item at Even Position.

In this program we will be taking user defined array, after taking array input we will find elements that are at even position and will display those elements. For this purpose we will traversing array and increment value of i by 2.

Algorithm/Program Explanation.

  • Start
  • Initialize array let say,
    • int array[]= new int array[size];
  • implement logic to print even position elements
    • for(int i=1;i<array.length;i+2)
  • Print all even position element till array size.
  • end
/* 
java program to find elements at even position */
import java.util.Scanner;
public class EvenPosArray{
 public static void main(String args[])
 { int size;
   System.out.println("enter the size of array");
   size=sc.nextInt();
   int array[]= new int[size];
   System.out.println("Enter Array Elements:");
   for(int i=0;i<size;i++)
   {
    array[i]=sc.nextInt();
   }sc.close();
    System.out.println("Elements at Even Positions are:");
   for(int i=1;i<array.length;i+2){
   System.out.println(array[i]);
   }
 }
}
Output:
enter the size of array
5
Enter Array Elements:
56 67 68 79 56
Element at Even position are
67
79

Program to Display Array Elements at Odd Position

In this program we will be taking user defined array, after taking array input we will find elements that are at Odd position and will display those elements. For this purpose we will traversing array and increment value of i by 2.

Algorithm/Program Explanation

  • Start
  • Initialize array let say,
    • int array[]= new int array[size];
  • implement logic to print even position elements.
    • for(int i=0;i<array.length;i+2)
  • Print all Odd position element till array size.
  • end
/* 
java program to find elements at Odd position */
import java.util.Scanner;
public class OddPosArray{
 public static void main(String args[])
 { int size;
   System.out.println("enter the size of array");
   size=sc.nextInt();
   int array[]= new int[size];
   System.out.println("Enter Array Elements:");
   for(int i=0;i<size;i++)
   {
    array[i]=sc.nextInt();
   }sc.close();
    System.out.println("Elements at Even Positions are:");
   for(int i=0;i<array.length;i+2)//odd logic i=0;
   {
   System.out.println(array[i]);
   }
 }
}
Output:
enter the size of array
5
Enter Array Elements:
10 20 30 40 50
Elements at Odd Positions are:
10
30
50


Program To find array element at both even and odd position

/* 
Java Program to find even odd positioned elements  */
import java.util.Scanner;
public class EvenOddPosArray{
 public static void main(String args[])
 { int size;
   System.out.println("enter the size of array");
   size=sc.nextInt();
   int array[]= new int[size];
   System.out.println("Enter Array Elements:");
   for(int i=0;i<size;i++)
   {
    array[i]=sc.nextInt();
   }sc.close();
    System.out.println("Elements at Even Positions are:");
   for(int i=1;i<array.length;i+2)//odd logic i=0;
   {
   System.out.println(array[i]);
   }
   System.out.println("Elements at Even Positions are:");
   for(int i=0;i<array.length;i+2)//odd logic i=0;
   {
   System.out.println(array[i]);
   }
 }
}
Output:
enter the size of array
7
Enter Array Elements:
10 23 45 78 86 89 56
Elements at Even Positions are:
23
78
89
Elements at Odd Positions are:
10
45
86
56

Important Questions asked in this topic are:

  • Write a program to display array elements at even position?
  • Write a program to display array elements at odd position?
  • Write a program to find array elements at both even & odd position?

Leave a Comment