-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathLinearSearch.java
More file actions
41 lines (39 loc) · 938 Bytes
/
Copy pathLinearSearch.java
File metadata and controls
41 lines (39 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.*;
public class LinearSearch
{
public static void main(String[] args) {
int index,arrayLength,elementToBeSearched,elementLocation=-1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of array");
arrayLength=sc.nextInt();
int array[]=new int[arrayLength];
System.out.println("Enter Elements of array");
for(index=0;index<arrayLength;index++)
array[index]=sc.nextInt();
System.out.println("Enter element to be searched");
elementToBeSearched=sc.nextInt();
for(index=0;index<arrayLength;index++)
{
if(array[index]==elementToBeSearched)
{
elementLocation=index+1;
}
}
if(elementLocation!=-1)
System.out.println("Element found at location "+elementLocation);
else
System.out.println("Element not found");
}
}
/*
Input/Output -
Enter size of array
3
Enter Elements of array
1
3
2
Enter element to be searched
2
Element found at location 3
*/