OneCompiler

Write a program to accept the count of numbers and the search element to find the position of the element using linear search.

156

import java.util.*;
public class LinearSearch1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE COUNT OF NUMBERS");
int n = sc.nextInt();
System.out.println("ENTER "+ n + " NUMBERS : ");
int a[] =new int[n];
for(int i=0; i<n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("ENTER THE ELEMENT TO BE SEARCHED : ");
int sr = sc.nextInt();
for(int i=0;i<n;i++)
{
if(a[i]==sr)
System.out.println("SEARCH ELEMENT FOUND AT " + (i+1));
else
System.out.println("ELEMENT NOT FOUND");
}
}
}