Friday, April 15, 2011

Sequential Search 1


import java.io.*;
import java.util.*;
class seqsearch
{
public static void main(String arg[])
{    
      int a[]=new int[50];
      boolean flag=false;
                  DataInputStream in=new DataInputStream(System.in);
try{
                               
                    System.out.println("Enter the no of elements in d array");
                    int n=Integer.parseInt(in.readLine());
                    for(int i=0;i<n;i++)
                    {
                                  System.out.println("Enter the "+(i+1)+" element of d array");
                                  a[i]=Integer.parseInt(in.readLine());
                    }
                    System.out.println("Enter the element to be searched");
                    int num=Integer.parseInt(in.readLine());
                    for(int i=0;i<n;i++)
                    {
                                  if(a[i]==num)

                                  {
                                    flag=true;
                                    System.out.println("Element found at pos"+(i+1));
                                    break;
                      }
                      else
                      flag=false;
                    }
                    if(flag==false)
                                System.out.println("Element not found");
 }
 catch(Exception e){}
}
}

No comments:

Post a Comment