Answer:
// here is code in java.
import java.util.*;
// class definition
class Solution
{
// function to perform sequential search
public static boolean item_search(int [] arr,int k)
{
boolean flag=false;
for(int a=0;a<arr.length;a++)
{
// if item found then return true,else false
if (arr[a]==k)
{
flag=true;
break;
}
}
return flag;
}
// main method of the classs
public static void main (String[] args) throws java.lang.Exception
{
try{
// variables
int size,item;
// scanner object to read input from user
Scanner scr=new Scanner(System.in);
//ask user to enter size
System.out.println("size of array:");
// read the size of array
size=scr.nextInt();
// create an array of given size
int inp_arr[]=new int[size];
System.out.println("enter the elements of array:");
// read the elements of the array
for(int x=0;x<size;x++)
{
inp_arr[x]=scr.nextInt();
}
System.out.println("enter the item to search:");
// read the item to be searched
item=scr.nextInt();
// call the function with array and item as arguments
System.out.println(item_search(inp_arr,item));
}catch(Exception ex){
return;}
}
}
Step-by-step explanation:
Read the size of array.Then create an array of the given size.Read the elements of the array.Then read the item to be searched.Call the method item_search() with array and item as arguments.Here check all the elements, whether the item is equal to any of the element of array or not.If any element is equal to item then method will return true otherwise return false.
Output:
size of array:5
enter the elements of array:3 6 1 8 9
enter the item to search:8
true