Computer Applications
Write a program to search for an integer value input by the user in the list given below using linear search technique. If found display "Search Successful" and print the index of the element in the array, otherwise display "Search Unsuccessful".
{75, 86, 90, 45, 31, 50, 36, 60, 12, 47}
Answer
import java.util.Scanner;
public class KboatLinearSearch
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int arr[] = {75, 86, 90, 45, 31, 50, 36, 60, 12, 47};
int l = arr.length;
int i = 0;
System.out.print("Enter the number to search: ");
int n = in.nextInt();
for (i = 0; i < l; i++) {
if (arr[i] == n) {
break;
}
}
if (i == l) {
System.out.println("Search Unsuccessful");
}
else {
System.out.println("Search Successful");
System.out.println(n + " present at index " + i);
}
}
}
Output
Related Questions
Define a class pin code and store the given pin codes in a single dimensional array. Sort these pin codes in ascending order using the Selection Sort technique only. Display the sorted array.
110061, 110001, 110029, 110023, 110055, 110006, 110019, 110033
Consider the following program segment and answer the questions given below:
int x[][] = {{2,4,5,6}, {5,7,8,1}, {34, 1, 10, 9}};
(a) What is the position of 34?
(b) What is the result of x[2][3] + x[1][2]?
Define a class to search for a value input by the user from the list of values given below. If it is found display the message "Search successful", otherwise display the message "Search element not found" using Binary search technique.
5.6, 11.5, 20.8, 35.4, 43.1, 52.4, 66.6, 78.9, 80.0, 95.5.
Which of the following is a valid way to declare and initialize an integer array to store the ages of 50 students?