Latest News: Java Program for Linear Search in the Array - Linear Search in Array Java - Linear Search in Array Algorithm

Java Program for Linear Search in the Array - Linear Search in Array Java - Linear Search in Array Algorithm

 Java Program for Linear Search in the Array - Linear Search in Array Java - Linear Search in Array Algorithm



import java.util.Scanner;
class LSearch
{
   public static void main(String args[])
   {
      int count , n, item, array[];
     
      Scanner input = new Scanner(System.in);
      System.out.println("Enter number of elements:");
      n = input.nextInt(); 
      
      array = new int[n]; 
      System.out.println("Enter " + n + " integers");
      
      for (count = 0; count < n; count++)
        array[count] = input.nextInt();

      System.out.println("Enter the search value:");
      item = input.nextInt();

      for (count = 0; count < n; count++)
      {
         if (array[count] == item) 
         {
           System.out.println(item+" is present at location "+(count+1));
           
           break;
         }
      }
      if (count == n)
        System.out.println(item + " doesn't exist in array.");
   }
}


Popular Posts

MyMarket