import java.util.*; public class LinearSearch{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int count=0; System.out.println("Enter size of an array"); int n=sc.nextInt(); int[] arr=new int[n]; System.out.println("Enter "+n+ " elements"); for(int i=0;i<arr.length;i++) { arr[i]=sc.nextInt(); } System.out.println("Enter key value to search"); int target=sc.nextInt(); for(int ele:arr) { if(ele==target) count++; System.out.println("ELEMENT FOUND "); } System.out.println("Arrays elements are"+Arrays.toString(arr)); System.out.println("KEY VALUE :"+target); //System.out.println("number of times it repeats >"+target+"are"+count); System.out.println("element found at index :"+arr[target]); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter