About

Kannur University btech CSE study materials, question papers, syllabus . . .

Thursday, July 4, 2013

S4- dsa lab - LINEAR SEARCH

/*                     LINEAR SEARCH               */
        import java.io.*;
class Lin
{
int a[],n,key,i;
DataInputStream nn=new DataInputStream(System.in);
int loc[];
void read()
{
try
{
System.out.print("Enter the limit : ");
n=Integer.parseInt(nn.readLine());
loc=new int[n];
a=new int[n];
System.out.println("Enter the elements: ");
for(i=0;i<n;i++)
a[i]=Integer.parseInt(nn.readLine());
System.out.print("Enter the no. tobe found out: " );
key=Integer.parseInt(nn.readLine());
}
catch(Exception e)
{}
}
void search()
{
int f=0,j=0;
for(i=0;i<n;i++)
{
if(key==a[i])
{
loc[j]=i+1;
f=1;
j++;
}
}


if(f==1)
{
System.out.print("no. is found at location :");
for(i=0;i<j;i++)
System.out.print(loc[i]);
}
else
System.out.println("no. is not found ");
}}
class Search
{
public static void main(String args[])
{
Lin obj=new Lin();
obj.read();
obj.search();
}
}

       OUTPUT

Enter the limit : 5
Enter the elements:
1
2
3
4
5
Enter the no. tobe found out: 4
no. is found at location :4

Enter the limit : 2
Enter the elements:
1
2
Enter the no. tobe found out: 3
no. is not found


0 comments:

Post a Comment