adsense


Showing posts with label JAVA PROGRAM FOR LINEAR SEARCH. Show all posts
Showing posts with label JAVA PROGRAM FOR LINEAR SEARCH. Show all posts

Monday, 16 November 2015

JAVA PROGRAM FOR LINEAR SEARCH PAGE 28








COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA PROGRAM FOR LINEAR SEARCH







Java program for linear search


Java program for linear search: Linear search is very simple, To check if 
an element is present in the given list we compare search element with 
every element in the list. If the number is found then success occurs 
otherwise the list doesn't contain the element we are searching.


Java programming code
 
import java.util.Scanner;
 
class LinearSearch 
{
  public static void main(String args[])
  {
    int c, n, search, array[];
 
    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt(); 
    array = new int[n];
 
    System.out.println("Enter " + n + " integers");
 
    for (c = 0; c < n; c++)
      array[c] = in.nextInt();
 
    System.out.println("Enter value to find");
    search = in.nextInt();
 
    for (c = 0; c < n; c++)
    {
      if (array[c] == search)     /* Searching element is present */
      {
         System.out.println(search + " is present at location " + (c + 1) + ".");
          break;
      }
   }
   if (c == n)  /* Searching element is absent */
      System.out.println(search + " is not present in array.");
  }
}