-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLinearSearch.java
More file actions
27 lines (26 loc) · 841 Bytes
/
Copy pathLinearSearch.java
File metadata and controls
27 lines (26 loc) · 841 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
package CP;
import java.util.Scanner;
public class LinearSearch {
public static int Ls(int[] input, int key){
int pos = 0;
for(int i=0 ; i<input.length ; i++){
if(key == input[i]){
System.out.println("the key is found at index : "+i);
}
}
return 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements nin the array:");
int n = sc.nextInt();
int[] input = new int[n];
System.out.println("Enter the array elements:");
for(int i=0 ; i<n ; i++){
input[i] = sc.nextInt();
}
System.out.println("Enter the key:");
int key = sc.nextInt();
int found = Ls(input, key);
}
}