Signup/Sign In
LAST UPDATED: MARCH 5, 2021

Java Program To Print the kth Element of Array

In this tutorial, we will learn how to print the kth element in an array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input:

Array Elements: 4 6 2 1 8 9 4 3 6 8 9

Enter the position whose element you want to know: 5

Output: Element at position 5 is 8

Program 1: Print the kth Element in an Array

In this case, we will see how to print the kth element in an array when values are pre-defined in the program. This means the values are already defined in the program and our task here is to write a program such that it will print the element present at the specified position.

Algorithm

  1. Start
  2. Declare an array.
  3. Initialize the array in the program.
  4. Print the elements of the array.
  5. Declare a variable that will store the value of the position.
  6. Initialize the position whose element you want to know.
  7. Print the element in that position.
  8. Stop

Below is the code for the same.

The below program demonstrates how to print the kth element in an array when the values are pre-defined.

// Java Program to Print the kth Element in the Array with pre defined elements
  
import java.io.*; 
import java.util.Scanner; 
  
public class Main 
{ 
    public static void main(String[] args) 
    { 
            // declare and intialize the array 
            int a[] = {3, 5, 7, 1 , 2, 9 , 8 , 1 , 4, 7 };
            //Print the Array elements
            System.out.println("Elements in the array ");
            for(int i=0;i<a.length;i++)
            {
                System.out.print(a[i]+" ");
            }
            System.out.println("");
            System.out.println("The position at which you want to check number:"); 
            
            int k = 5;
            
            //Print the element at kth  position 
            if(k<a.length)
            {
            System.out.println("Element at "+ k +"th position is " + a[k - 1]); 
            }
            else
            {
                System.out.println("Enter valid position");
            }
    } 
}


Elements in the array
3 5 7 1 2 9 8 1 4 7
The position at which you want to check number:
Element at 5th position is 2

Program 2: To Print the kth Element

In this case, we will see how to print the kth element in an array when values are user-defined in the program. This means that here the values are provided by the user and our task here is to write a program such that it will print the element present at the specified position.

Algorithm

  1. Start
  2. Declare an array size.
  3. Ask the user to initialize the array size
  4. Declare an array.
  5. Ask the user to initialize the array elements.
  6. Declare a variable to store the position of the array.
  7. Ask the user to initialize the array position.
  8. Print the element at that position.
  9. Stop

Below is the code for the same.

The below program demonstrates how to print the kth element in an array. We will ask the user to enter the value of the position whose element wants to know.

// Java Program to Print the kth Element in the Array with User Defined elements
  
import java.io.*; 
import java.util.Scanner; 
  
public class Main 
{ 
    public static void main(String[] args) 
    { 
            int n; 
  
            // scanner object to acces user input 
            Scanner s = new Scanner(System.in); 
            System.out.print("Enter the number of elements in the array:"); 
            // Ask the user to initialize the array size
            n = s.nextInt(); 
  
            // declare an array 
            int a[] = new int[n]; 
            System.out.println("Enter all the elements of the array:"); 
  
            // Ask the user to initializing the array elements using a for loop
            for (int i = 0; i < n; i++) 
            { 
                a[i] = s.nextInt(); 
            } 
  
            System.out.println("Enter the position at which you want to check number:"); 
            //Ask the user to intialize the position
            int k = s.nextInt(); 
            
            //Print the element at kth  position 
            System.out.println("Element at "+ k +"th position is " + a[k - 1]); 
    } 
}


Enter the number of elements in the array: 10
Enter all the elements of the array: 2 3 4 6 1 2 9 8 7 6
Enter the position at which you want to check number: 4
Element at 4th position is 6



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.