-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlinear-search.java
32 lines (32 loc) · 923 Bytes
/
linear-search.java
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
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{ public static void main(String[] args) {
int[] myarray = {21,43,23,54,75,13,5,8,25,10};
int key,location=0;
Scanner sc = new Scanner(System.in);
System.out.println("The input array is");
for(int i=0;i<10;i++){
System.out.print(myarray[i]+" ");
}
System.out.println("\n");
System.out.println("Enter key");
key = sc.nextInt();
for(int i = 0; i<10; i++)
{
if(myarray[i]==key)
{
location = i+1; break;
}
else
location = 0;
}
if(location != 0)
{
System.out.println("key found at location " + location);
}
else
System.out.println("Key not found");
}
}