-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathSearching.java
More file actions
37 lines (35 loc) · 741 Bytes
/
Searching.java
File metadata and controls
37 lines (35 loc) · 741 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
import java.util.Scanner;
public class Searching {
public static int search(int arr[], int x) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int arr[] = {
10,
20,
30,
40,
50,
60,
70,
80,
90,
100
};
System.out.print("Enter the number: ");
int x = input.nextInt();
System.out.println();
int result = search(arr, x);
if (result == -1) {
System.out.println("Sorry, data not found");
} else {
System.out.println("Number " + x + " in index " + search(arr, x) + "");
}
}
}