-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.java
More file actions
36 lines (31 loc) · 854 Bytes
/
LinearSearch.java
File metadata and controls
36 lines (31 loc) · 854 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
import java.util.*;
public class LinearSearch {
public static void main(String[] args) {
Scanner scan =new Scanner(System.in);
int a,b,c,search;
System.out.println("Enter the size of arrays ");
a=scan.nextInt();
if(a <= 0) {
System.out.println("Array size must be greater than 0.");
return;
}
int arr[]=new int[a];
for (int i=0;i<a;i++)
{
System.out.println("Enter number "+(i+1)+" element of array: ");
arr[i]=scan.nextInt();
}
System.out.println("Enter the number you want to search: ");
search=scan.nextInt();
for ( c = 0 ; c < a ; c++ )
{
if ( arr[c] == search )
{
System.out.println(search+" is present at location "+(c+1));
break;
}
}
if ( c == a )
System.out.println(search+" is not present in array.");
}
}