How to find out nth value by using array in Java | java code examples with output
3 min
Ankaj Gupta
March 23, 2021

How to find out nth value by using array in Java | java code examples with output

single level inheritance

# Java program to find out n'th value by using array

In this section, You will learn how to find out nth value by using array in Java.

Let's try to create a simple example :

  ➤ Example : student.java;

  1. import java.io.*;

  2. import java.util.Scanner;

  3. public class FindElement {

  4.   public static void main(String args[]) {

  5. int[] arr = {5, 10, 15, 20, 23};

  6.   

  7. Scanner input = new Scanner(System.in);

  8. System.out.print("Enter the nth position: ");

  9. int num = input.nextInt();

  10.   

  11. if(num <= arr.length){

  12.   int element = arr[num-1];

  13.   System.out.println("Element is : "+element);

  14. }

  15. else{

  16.   System.out.print("Enter any position upto "+arr.length);

  17. }

  18.   }

  19. }

Output :
  • Enter the nth position: 10

  • Enter any position upto 5


  • ---------------------------------------------------------------------

  • Enter the nth position: 4

  • Element is : 20

Recommended Posts:
Java program Java programming

Related Posts