Linear search ~ myTechpartner Join Me On Facebook
Join Me On Facebook

Blogroll

Sunday, 7 April 2013

Linear search

import java.io.*;
class linear
{
public static void main(String arg[]) throws Exception
{
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("How many elements");
  int n=Integer.parseInt(br.readLine());
  int a[]=new int[n];
  System.out.println("Enter the elements");
  for(int i=0;i<n;i++)
    {
     a[i]=Integer.parseInt(br.readLine());
    } 
  System.out.println("The array is:");
  for(int i=0;i<n;i++)
  {
   System.out.println(a[i]);
  }
  System.out.println("Enter the element to be search");
     int k=0;
     int j=Integer.parseInt(br.readLine());
     for(int i=0;i<n;i++)
     {
      if(a[i]==j)
       k++;
     }
    
     if(k==0)
      System.out.println("Not found");
     else
      System.out.println("Found");
}
}
Follow this step to compile and run your code in CMD
1.Copy this and paste it into notepad and save this as class name(here linear) with extension .java      :- linear.java
  Remember where this file you save.
2. Open cmd and go to the folder ,file that you save linear.java
3.Type    javac linear.java       eg:-D:\ajith_er\labjava>javac linear.java
  if any error that will print on screen
4. then type      java linear     eg:-D:\ajith_er\labjava>java linear
now your program will be start to execute
If any help email me my id: ajitheredacheril@hotmail.com or ajither.9@gmail.com                                                                                                                                                                                                            by :- aer

0 comments:

Post a Comment

 

Contributors

Online Marketing

Do you Like this Article?

rss twitter facebook