-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArray_Ques1.java
31 lines (26 loc) · 1000 Bytes
/
Array_Ques1.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
31
import java.util.Scanner;
public class Array_Ques1 {
public static void main(String[] args) {
// Linear Search Algorithm and other algorithms
// Take an Array as input. Search for the given number x
// and print the index at which it occurs.
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of Array: ");
int size = sc.nextInt();
int[] new_array = new int[size];
System.out.println("Enter the elements of the Array");
for (int i = 0;i<size;i++){
new_array[i] = sc.nextInt();
}
// All the elements are entered now suppose we have find the
// find the index of any number
System.out.print("Enter a number from the array : ");
int num = sc.nextInt();
for (int i = 0; i<size; i++){
if (new_array[i]==num){
System.out.printf("The index of %d is %d\n",num,i);
}
}
sc.close();
}
}