-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearSearch.java
More file actions
32 lines (28 loc) · 877 Bytes
/
linearSearch.java
File metadata and controls
32 lines (28 loc) · 877 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
package HW_26_January;
import java.util.Scanner;
public class linearSearch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter array size: ");
int n = sc.nextInt();
int[] arr = new int[n];
System.out.print("Enter array elements: ");
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
System.out.print("Enter the element to search: ");
int x = sc.nextInt();
boolean flag = false;
for (int i = 0; i<n; i++) {
if (arr[i] == x) {
flag = true;
break;
}
}
if (flag == true) {
System.out.println("Element found.");
} else {
System.out.println("Element not found.");
}
}
}