-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlinersearch.java
More file actions
39 lines (39 loc) · 925 Bytes
/
linersearch.java
File metadata and controls
39 lines (39 loc) · 925 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
import java.util.*;
class LinearSearch
{
public static int LinearSearch(int array[],int k,,int search)
{
int i=0;
for(i=0;i<k;i++)
{
if(array[i]==search)
{
//Here program searches for the required data if the data is found then it
returns the place of data in the array otherwise it returns -1 which means not
found.
return i;
}
}
return -1;
}
public static void main(String [] args)
{
Scanner in=new Scanner(System.in);
System.out.println(“Enter the number of elements you want to have in
array”);
int k=in.nextInt();
int array[]=new int[k];
System.out.println(“Enter the elements”);
int i;
for(i=0;i<k;i++)
{ //Getting data from
user
array[i]=in.nextInt();
}
System.out.println(“Enter the element that you want to search in array”);
int search=in.nextInt();
// passing array ,integer k,and integer search
int place =LinearSearch(array,k,search);
System.out.println(“Data is present at place”+place);
}
}