forked from LalinduWenasara/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearSearch.java
More file actions
40 lines (35 loc) · 1003 Bytes
/
LinearSearch.java
File metadata and controls
40 lines (35 loc) · 1003 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
40
package linearsearch;
import java.util.Scanner;
public class LinearSearch {
public static void main(String args[]){
int ar[] = new int[10];
int i, number, m, d=0, position=0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter your size of array");
m = sc.nextInt();
System.out.println("Enter your array");
for(i=0; i<m; i++)
{
ar[i] = sc.nextInt();
}
System.out.println("what is the number you want to search position");
number = sc.nextInt();
for(i=0; i<m; i++)
{
if(ar[i] == number)
{
d = 1;
position = i+1;
break;
}
}
if(d == 0)
{
System.out.println("Number Not Found..!! please try again!");
}
else
{
System.out.println(number+ " Position is: " + position);
}
}
}