Skip to content

Commit 5d7d8d3

Browse files
authored
Create LinearSearch.java
1 parent 8ecfcf3 commit 5d7d8d3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

LinearSearch.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)