You are on page 1of 1

c  


Y
#include<conio.h>
#include<stdio.h>

int lsearch(int ar[],int size,int item) //function to perform linear search


{
for(int i=0;i<size,i++)
{
if(ar[i]==item)
return I;
}
return -1; //the control will reach here only if the item is not found

void main()
{
in tar[50],item,n,index;
clrscr();
printf(͞\n ENTER DESIRED ARRAY SIZE(MAX 50)-:͟);
scanf(͞%d͟,&n);
printf(͞\n ENTER ARRAY ELEMENTS-:\n͟);
for(int i=0;i<n;i++)
{
scanf(͞%d͟,ar[i]);
}
printf(͞\n ENTER ELEMENT TO BE SEARCHED FOR-:͟);
scanf(͞%d͟,&item);
index=lsearch(ar,n,item);
if(index==-1)
printf(͞\n SORRY!GIVEN ELEMENT COULD NOT BE FOUND.\n͟);
else
printf(͞\n ELEMENT FOUND AT INDEX :%d, POSITION :%d͟,index,(index+1));

getch();
}

·  
ENTER DESIRED ARRAY SIZE(MAX 50)-:7
ENTER ARRAY ELEMENTS-

1 5 2 8 6 10 7
ENTER ELEMENT TO BE SEARCHED FOR-:6
ELEMENT FOUNT AT INDEX :4, POSITION :5