Skip to content

Commit a0a0d34

Browse files
authored
Merge pull request #96 from eby8zevin/patch-1
Create Searching.java
2 parents e40589c + 56aa950 commit a0a0d34

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Searching.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.Scanner;
2+
public class Searching {
3+
public static int search(int arr[], int x) {
4+
for (int i = 0; i < arr.length; i++) {
5+
if (arr[i] == x) {
6+
return i;
7+
}
8+
}
9+
return -1;
10+
}
11+
public static void main(String[] args) {
12+
Scanner input = new Scanner(System.in);
13+
int arr[] = {
14+
10,
15+
20,
16+
30,
17+
40,
18+
50,
19+
60,
20+
70,
21+
80,
22+
90,
23+
100
24+
};
25+
System.out.print("Enter the number: ");
26+
int x = input.nextInt();
27+
System.out.println();
28+
29+
int result = search(arr, x);
30+
if (result == -1) {
31+
System.out.println("Sorry, data not found");
32+
} else {
33+
System.out.println("Number " + x + " in index " + search(arr, x) + "");
34+
}
35+
}
36+
37+
}

0 commit comments

Comments
 (0)