OneCompiler

SEARCHING for an element in an array

148

Write a program which searches an element from an array and locates the position of the searched element.

import java .util.*;
class search
{
public static void main(String args[])
{
int i;
int[] array = new int[50];
int look = 0;
int location = 0;
Scanner sc = new Scanner (System.in);
System.out.println("HOW MANY NUMBERS : ");
int n= sc.nextInt();
for( i = 0; i<n; i++)
{
System.out.print("ENTER NUMBER = " + (i+1) + " :: ");
array[i] = sc.nextInt();
}
System.out.println("ENTER NUMBER TO BE SEARCHED = ");
look = sc.nextInt();
for( i =0 ; i<n;i++)
{
if(array[i]==look)
location = i+1;
}
if(location>0)
System.out.println("THE REQUIRED NUMBER : " + look + " FOUND AT = " + location);
else
System.out.println("THE REQUIRED NUMBER : " + look + " NOT FOUND !!");
}
}

FOR CODE ,
CLICK : https://onecompiler.com/java/3xymve8c5 (TO FIND OUTPUT)