-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearching.java
More file actions
52 lines (41 loc) · 1.09 KB
/
Searching.java
File metadata and controls
52 lines (41 loc) · 1.09 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner; // input output
// DONE
public class Searching
{
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in); // input
int [] array = new int[5]; // made array of 5 integers
// INPUT
int i = 0;
while(i < 5)
{
System.out.print(" Enter the " + (i+1) + " Number : ");
array[i] = obj.nextInt();
i++;
}
System.out.println(); // SPACING
// searching
System.out.print(" Enter the number you want to search : ");
int search = obj.nextInt();
int j = 0; // manual gear
boolean found = false; // number isn't found yet
while (j < 5)
{
if (array[j] == search) {
found = true;
break;
}
j++;
}
// added efficient code
if (found)
{
System.out.println(search + " is found at i = " + (j+1));
}
else
{
System.out.println(" SORRY! number not found");
}
}
}