Enter size of array and then enter all the elements of that array. Now enter the position k at which you want to find element. Now we print the element at k-1 position in given array.
Here is the source code of the Java Program to Print the kth Element in the Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
public class Position
public static void main(String args)
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
a[i] = s.nextInt();
System.out.print("Enter the k th position at which you want to check number:");
int k = s.nextInt();
$ javac Position.java $ java Position Enter no. of elements you want in array:5 Enter all the elements: 2 5 3 8 6 Enter the k th position at which you want to check number:3 Number:3
Sanfoundry Global Education & Learning Series – 1000 Java Programs.