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.
import java.util.Scanner;
public class Position
{
public static void main(String[] args)
{
int n;
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();
System.out.println("Number:"+a[k-1]);
}
}
Output:
$ 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.
advertisement
advertisement
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Practice BCA MCQs
- Apply for Java Internship
- Practice Programming MCQs