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".
Linear Search Program in Java
Answer:
Linear Search Program in Java
import java.util.Scanner;
public class KboatLinearSearch
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
double arr[] = new double[20];
int l = arr.length;
int i = 0;
System.out.println("Enter array elements: ");
for (i = 0; i < l; i++)
{
arr[i] = in.nextDouble();
}
System.out.print("Enter the number to search: ");
double n = in.nextDouble();
for (i = 0; i < l; i++)
{
if (arr[i] == n)
{
break;
}
}
if (i == l)
{
System.out.println("Not found");
}
else
{
System.out.println(n + " found at index " + i);
}
}
}