Code is copied!
Define a class to accept values into an array of double data type of size 20. Accept a double
value from user and search in the array using linear search method. If value is found ,
display message “Found” with its position where it is present in the array. Otherwise
display message “not found”.
Solution:
import java.util.*;
class LinearSearch
{
public static void main(String args[])
{
double a[] = new double[20];
Scanner sc = new Scanner(System.in);
System.out.println("Enter 20 values in Array");
for(int i=0; i< 20; i++)
{
a[i] = sc.nextDouble();
}
System.out.println("Enter the value to be searched?");
double x = sc.nextDouble();
int pos = -1;
for(int i=0; i< a.length; i++)
{
if(a[i] == x)
{ pos = i; break;
}
}
if(pos != -1)
System.out.println("Element is found at "+(pos+1));
else
System.out.println("Element is not found.");
}
}