CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

Search in the array using linear search

JAVA November 19, 2025 6 views 0 likes
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".

Answer:


Linear Search Program in Java

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);
        }
    }
}
6 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285