File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .*;
2
+
3
+ class LinearSearch
4
+ {
5
+ public static void main (String args [])
6
+ {
7
+ int c ,n ,search ,array [];
8
+ Scanner sc = new Scanner (System .in );
9
+
10
+ System .out .println ("Enter the number of elements in an array :- " ); //Inputting values to be entered in an array
11
+ n = sc .nextInt ();
12
+ array = new int [n ]; // Creating an array according to the size specified by the user
13
+
14
+ System .out .println ("Enter " + n +" integers to be put in array :- " );
15
+ for (int c = 0 ; c < n ; c ++)
16
+ {
17
+ array [c ] = sc .nextInt ();
18
+ }
19
+ System .out .println ("Enter the value to find :- " );
20
+ search = sc .nextInt ();
21
+
22
+ for (c = 0 ; c < n ; c ++)
23
+ {
24
+ if (array [c ] == search ) // If the element is present
25
+ {
26
+ System .out .println (search + "is present at location " + (c +1 ) + "." );
27
+ break ;
28
+ }
29
+ }
30
+ if (c == n )
31
+ {
32
+ System .out .println (search + "is not present at in the array" );
33
+ }
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments