package LinearSearch; import java.util.*; public class LinearSearch { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int arr[] = { 2, 3, 4, 10, 40 }; System.out.print("What element are you searching for? "); int x = sc.nextInt(); int result = search(arr, arr.length, x); if (result == -1) System.out.print("Element is not present in array"); else System.out.println("Element is present: " + arr[result]); } public static int search(int arr[], int N, int x) { for (int i = 0; i < N; i++) { if (arr[i] == x) return i; } return -1; } }
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