- 2

How do I solve this using java?

Write a program that allows the user to determine the length of the array, enter its elements, enter the target element for the search, and then apply the linear search algorithm to it.

28th Sep 2021, 5:27 PM
Mashael
2 odpowiedzi
0
Please show your attempt, so that we can help you.
28th Sep 2021, 5:43 PM
Rupali Haldiya
Rupali Haldiya - avatar
0
import java .util.Scanner; Public class Sally{ Public static void main(String args[]) { int counter,num,item,array[]; //To captuer user input Scanner input = new Scanner(System.in); System.out.println(“Enter number of elements:”); num = input.nextlnt(); //creating array to store the all the numbers array = new int[num]; System.out.prentln(“Enter” + num +”integers”); //loop to store each numbers in array for (counter =0; counter< num; counter++) array[counter] = input.nextlnt(); System.out.println(“Enter the search value:”); item = input.nextlnt(); For ( counter =0; counter<num; counter ++) { if(array[counter] == item) { System.out.println(item+”is present at location”+counter); /*ltem is found so to stop the search and to come out of the loop use break statement./ break; } } if(counter == num) System.out.println(item + “doesn’t exist in array.”); } } and then apply the linear search algorithm to it
28th Sep 2021, 6:36 PM
Mashael