-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray1.java
30 lines (24 loc) · 855 Bytes
/
array1.java
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
//Take an array as input from the user.Search for a given number x and print the index at which it occurs.
//Linear Search
import java.util.*;
class arrays {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of an array: ");
int size = sc.nextInt();
// for loop to take inputs
System.out.println("Enter " + size + " numbers");
int num[] = new int[size];
for (int i = 0; i < size; i++) {
num[i] = sc.nextInt();
}
System.out.println("Enter any number you have to find: ");
int x = sc.nextInt();
// for loop to show output
for (int i = 0; i < size; i++) {
if (num[i] == x) {
System.out.println("x found at index :" + i);
}
}
}
}