-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLinearSearch.java
More file actions
29 lines (29 loc) · 865 Bytes
/
LinearSearch.java
File metadata and controls
29 lines (29 loc) · 865 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
import java.util.Scanner;
public class LinearSearch {
public static void main(String[] args) {
Scanner cc=new Scanner(System.in);
System.out.println("the array size is:");
int s= cc.nextInt();
int []arr=new int[s];
System.out.println("the array elements are:");
for(int i=0;i<s;i++)
{
arr[i]= cc.nextInt();
}
System.out.println("the element we have to search in the array is:");
int k=cc.nextInt();
int c=0;
for(int i=0;i<s;i++)
{
if(arr[i]==k)
{
c=i+1;
System.out.println("the key element"+k+" is present in the array\n in the position of"+c);
}
}
if(c==0)
{
System.out.println("the element is not present in the array");
}
}
}