Linear search



CODE:
import java.util.*;
class Search
{
     public static void main(String args[])
     {
           int i,n,search;
           Scanner sc = new Scanner(System.in);
           System.out.print("Enter number of elements:");
           n=sc.nextInt();
           int a[]=new int[n];
           for(i=0;i<=n-1;i++)
           {
                System.out.print("enter a no:");
                a[i]=sc.nextInt();
           }
           System.out.print("Enter the no to be searched:");
           search=sc.nextInt();
           for(i=0;i<=n-1;i++)
           {
                if(search==a[i])
                break;
           }
           if(i==n)
           System.out.println("no not found");
           else
           System.out.println("Index="+i);
     }
}
     
OUTPUT:
 

No comments :

Post a Comment