Jacob_Thomas

Linear Search

Jan 28th, 2021
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. import java.util.*;
  2. class LinearSearch{
  3.     public static void main(String args[]){
  4.         boolean flag = false;
  5.         Scanner sc = new Scanner(System.in);
  6.         System.out.println("Enter the size of array");
  7.         int n = sc.nextInt();
  8.         System.out.println("Enter the array");
  9.         int arr[] = new int[n];
  10.         for(int x = 0; x<n; x++){
  11.             arr[x] = sc.nextInt();
  12.         }
  13.         int key = sc.nextInt();
  14.         for(int x=0; x<n; x++){
  15.             if(arr[x] == key){
  16.                 flag = true;
  17.             }
  18.         }
  19.         if(flag == true){
  20.             System.out.println("Key found");
  21.         }
  22.         else{
  23.             System.out.println("Key not found");
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment