forked from darpanjbora/Java-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.java
More file actions
39 lines (32 loc) · 981 Bytes
/
LinearSearch.java
File metadata and controls
39 lines (32 loc) · 981 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.Scanner;
public class LinearSearch
{
public static int search(int elements[], int element_to_find)
{
for(int i = 0; i < elements.length; i++)
{
if(elements[i] == element_to_find)
return i;
}
return -1;
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of Elements you want");
int number_of_elements = sc.nextInt();
int elements []= new int[number_of_elements];
System.out.println("Enter the Element to find");
int finding_element = sc.nextInt();
System.out.println("Enter elements in Array");
for (int i = 0; i < number_of_elements; i++) {
elements[i]= sc.nextInt();
}
int result = search(elements, finding_element);
if(result == -1)
System.out.print("Element is not present in array");
else
System.out.print("Element "+ finding_element + " is present");
sc.close();
}
}