Define a class to declare an integer array of size n and accept the elements into the array. Search for an element input by the user using the linear search technique, display the element
Advertisements
प्रश्न
Define a class to declare an integer array of size n and accept the elements into the array. Search for an element input by the user using the linear search technique, display the element if it is found, otherwise display the message “NO SUCH ELEMENT".
थोडक्यात उत्तर
Advertisements
उत्तर
import java.util.Scanner;
class LinearSearch
{
public static void main(String args[])
{
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextlnt();
array= new int[n];
System.out.println("Enter those" +n+ "elements");
for (c = 0; c < n; c++)
array[c] = in.nextlnt();
System.out.println("Enter value to find");
search = in.nextlnt();
for (c = 0; c < n; c++)
{
if (array[c] = search) /*Searching element is present*/
{
System.out.println(array[c]);
break;
}
}
if (c = n) /*Element to search isn't present*/
System.out.println(search + "No such element");
}
}shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
