Custom Search

Saturday 16 June 2012

Linear Search Using JAVA


import java.io.*;
public class linearsearch
{
public static void main(String ar[])throws IOException
{
int a[]=new int[10];
int n,i,e,flag=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter the limit: ");
n=Integer.parseInt(br.readLine());
System.out.print("enter the elements: ");
for(i=0;i
{
a[i]=Integer.parseInt(br.readLine());
}
System.out.println("enter the element to be searched: ");
e=Integer.parseInt(br.readLine());
for(i=0;i
{
if(a[i]==e)
{
flag=1;
System.out.print("\nelement found in the position: ");
System.out.print(i+1);
}
}
if(flag==0)
{
System.out.print("\nelement not found");
}
}
}

No comments:

Post a Comment

LinkWithin

Related Posts Plugin for WordPress, Blogger...