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…

ICSE Class 10 Computer Applications PYQ · 2023 Paper · Section B

7Written · 2023 · Section B · 2023 Paper

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".

Show SolutionHide Solution↓
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);
        }
    }
}
BlueJ output of KboatLinearSearch.java
← Previous questionNext question →

Browse the free Computer Applications sample bank