This is a Java Program to Print the kth Element in the Array.
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.
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Practice Programming MCQs
- Apply for Information Technology Internship
- Practice Information Technology MCQs
- Buy Java Books
- Practice BCA MCQs