Welcome To Uday Satya Blog

25 . LINEAR SEARCH



/* ********************************************* ******
      25. TO SEARCH ELEMENT USING LINEAR SEARCH
    ************************************************* */
#include<*stdio.h>
#include<*conio.h>
int lsearch(int a[],int n,int key);
void main()
{
int a[10],i,n,key,l;
clrscr();
printf("\n enter the total number of elements : ");
scanf("%d",&n);
printf("\n enter the element to be serched : ");
for(i=0;i
{
scanf("%d",&a[i]);
}
printf("\n enter the element to be searched : ");
scanf("%d",&key);
l=lsearch(a,n,key);
if(l==-1)
printf("\n element is not found");
else
printf("\n element is found at the position : %d ",l+1);
getch();
}

int lsearch(int a[],int n,int key)
{
int t=-1,i;
for(i=0;i
if(a[i]==key)
t=i;
return t;
}

outputs

 enter the total number of elements : 5

 enter the elements : 1 2 3 4 5

 enter the element to be searched : 3

 element is found at position : 3

 enter the total number of elements : 5

 enter the elements : 1 2 3 4 5

 enter the element to be searched : 6

 element is not found

0 comments:

Post a Comment