Java Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 14: Write a Java program to display the elements at even position of an array.

EvenPosition.java

import java.util.Scanner;

public class EvenPosition
{
	public static void main(String[] args) 
	{
		int n;
		
		Scanner s = new Scanner(System.in);
		
		System.out.println("Enter the number of elements to be included in the array");
		n=s.nextInt();
		
		int a[] = new int[n];
		System.out.println("Enter elements of array");
	
		for(int i=0; i < n; i++)
		{
			System.out.print("Enter "+(i+1)+" element: ");
			a[i]=s.nextInt();
		}

		// Position of the element in array = index+1
		System.out.println("Elements present at even position: ");
		for(int i=0;i < n;i++)
		{
			if((i+1)%2==0)
			{
				System.out.println(a[i]);
			}
		}			
	}
}



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.