Ticker

6/recent/ticker-posts

Java (Search Position In Array)

SEARCH POSITION IN AN ARRAY



PROGRAM TO SEARCH POSITION IN AN ARRAY
import java.util.Scanner; //Import the Scanner class
class Search_position
{
public static void main(String args[])
{
int num[] = new int[60];
int i,n,size;

System.out.print("Enter array size: ");
Scanner in = new Scanner(System.in);
size=in.nextInt();

//Insert value in size variable at run time
//size=in.nextInt(); 

System.out.println("Enter values in array:");
//Scanner in = new Scanner(System.in);
for(i=0; i<size; i++)
{
num[i]=in.nextInt();
}

System.out.print("Enter value to search: ");
n=in.nextInt();

//Loop to find entered value position.
for(i=0; i<size; i++)
{
if(n == num[i])
break;
}
System.out.println("Value found at position: "+(i+1));
}
}


- - - PROGRAM OUTPUT - - -










Post a Comment

0 Comments