-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTarget element.java
More file actions
28 lines (26 loc) · 914 Bytes
/
Target element.java
File metadata and controls
28 lines (26 loc) · 914 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
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements in the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements of the array:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.print("Enter the element to search: ");
int target = scanner.nextInt();
boolean found = false;
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
System.out.println("Element " + target + " found at index " + i);
found = true;
break;
}
}
if (!found) {
System.out.println("Element " + target + " not found in the array.");
}
}
}