public class LinearSearch {
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Size of an Array :");
n=sc.nextInt();
int arr[] = new int[n];
int choice;
int flag = 0;
for (int i = 0; i < n; i++) {
System.out.print("Enter " + i + " index value :");
arr[i] = sc.nextInt();
}
System.out.print("Enter your Number you want to search :");
choice = sc.nextInt();
for (int i = 0; i < n; i++) {
if (choice == arr[i]) {
System.out.print("location " + i);
flag = 1;
}
}
if (flag == 1) {
System.out.println(" Number found.");
} else {
System.out.println("Number not found.");
}
}
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Size of an Array :");
n=sc.nextInt();
int arr[] = new int[n];
int choice;
int flag = 0;
for (int i = 0; i < n; i++) {
System.out.print("Enter " + i + " index value :");
arr[i] = sc.nextInt();
}
System.out.print("Enter your Number you want to search :");
choice = sc.nextInt();
for (int i = 0; i < n; i++) {
if (choice == arr[i]) {
System.out.print("location " + i);
flag = 1;
}
}
if (flag == 1) {
System.out.println(" Number found.");
} else {
System.out.println("Number not found.");
}
}
}
0 comments:
Post a Comment