File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .Scanner ;
2
+
3
+ public class LinearSearch
4
+ {
5
+
6
+ public static int search (int elements [], int element_to_find )
7
+ {
8
+ for (int i = 0 ; i < elements .length ; i ++)
9
+ {
10
+ if (elements [i ] == element_to_find )
11
+ return i ;
12
+ }
13
+ return -1 ;
14
+ }
15
+
16
+ public static void main (String args [])
17
+ {
18
+ Scanner sc = new Scanner (System .in );
19
+
20
+ System .out .println ("Enter the number of Elements you want" );
21
+ int number_of_elements = sc .nextInt ();
22
+ int elements []= new int [number_of_elements ];
23
+
24
+ System .out .println ("Enter the Element to find" );
25
+ int finding_element = sc .nextInt ();
26
+ System .out .println ("Enter elements in Array" );
27
+ for (int i = 0 ; i < number_of_elements ; i ++) {
28
+ elements [i ]= sc .nextInt ();
29
+ }
30
+
31
+ int result = search (elements , finding_element );
32
+ if (result == -1 )
33
+ System .out .print ("Element is not present in array" );
34
+ else
35
+ System .out .print ("Element " + finding_element + " is present" );
36
+
37
+ sc .close ();
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments