Computer Applications
Write a program to search for an ITEM linearly in array X[10].
Java
Java Arrays
3 Likes
Answer
import java.util.Scanner;
public class KboatLinearSearch
{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int X[] = new int[10];
int i = 0;
System.out.println("Enter array elements : ");
for(i = 0; i < 10; i++)
{
X[i] = in.nextInt();
}
System.out.println("Input Array is:");
for (i = 0; i < 10; i++) {
System.out.print(X[i] + " ");
}
System.out.println();
System.out.print("Enter ITEM to search : ");
int item = in.nextInt();
for (i = 0; i < 10; i++) {
if (X[i] == item) {
break;
}
}
if (i == 10) {
System.out.println("Search Unsuccessful");
}
else {
System.out.println("Search Successful");
System.out.println(item + " is present at index " + i);
}
}
}
Variable Description Table
Program Explanation
Output
![BlueJ output of Write a program to search for an ITEM linearly in array X[10]. BlueJ output of Write a program to search for an ITEM linearly in array X[10].](https://cdn1.knowledgeboat.com/img/sa10/c7-arrays-p43.jpg)
Answered By
1 Like
Related Questions
Explain
(i) Linear search method,
(ii) Binary search method.
Which of the two is more efficient for sorted data ?
Write a program Lower-left-half which takes a two dimensional array A, with size N rows and N columns as argument and prints the lower left-half.
2 3 1 5 0 7 1 5 3 1 e.g.,If A is 2 5 7 8 1 0 1 5 0 1 3 4 9 1 5 2 7 1 The output will be 2 5 7 0 1 5 0 3 4 9 1 5
Write a program to search for an ITEM using binary search in array X[10].
The following array of integers is to be arranged in ascending order using the bubble sort technique:
26 21 20 23 29 17
Give the contents of the array at the end of each iteration. Do not write the algorithm.