create a program using sequential search in c or java that f
create a program using sequential search in c++ or java that finds the key and index of the array where the key was found. If key not found output it not in the array. The range of numbers is 1-100
Solution
public class HelloSequential{
public static int sequentialSearch(int[] arr, int key){ //method for the operation sequential search,passing two parameters array and key
int size = arr.length;
int i=0;
while(i<size){
if(arr[i] == key){
System.out.println(\"Key \"+key+\" found at index: \"+i); //if key is found here printing the index of the found key
break;}
else
i++;
}
if(i==size) //handling the case when key is not found
System.out.println(\"Key is not in the array\");
return 0;
}
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
System.out.println(\"enter the size of the array\"); //asking for the size of input array from the user
int n=sc.nextInt();
int arr[]=new int[n]; //declaring input array
System.out.println(\"enter the values of the array,please enter the values within the range of 1 to 100 \ \"); //asking the input values from the user
for(int i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println(\"enter the search key\"); //asking the key to be searched from the user
int searchKey = sc.nextInt();
sequentialSearch(arr, searchKey); //calling the function
}
} //end of the code
***********Output************
enter the size of the array
5
enter the values of the array,please enter the values within the range of 1 to 100
1
5
7
4
12
enter the search key
8
Key is not in the array
*************Output************
Please let me know in case of any doubt.
