How to find out nth value by using array in Java? |EasyCoding45

How to find out nth value by using array in Java?


In this tutorial we are going to learn How to find out nth value by using array in Java?.


Note :-

  • Array index starts from 0.

Program :-(How to find out nth value by using array in Java?)


import java.util.*;
class value
{
Scanner sc=new Scanner(System.in);
int pos;
public void search(int n)
{

int arr[]=new int[n];
System.out.println("Enter Elements of Array :");
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
System.out.print("Array is :");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");

}
System.out.println("\nEnter The Position of Element you want to Print :");
pos=sc.nextInt();
if(pos<0 || pos>n)
{
System.out.println("Position out of Range :");
}
else
{
System.out.println("Element at that position is :"+arr[pos]);
}
}
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter How many Elements you want to Store :");
int no=s.nextInt();
value v=new value();
v.search(no);
}
}

Output :-




Comments