Linear Search Using Recursion
Program Code:
#include<stdlib.h>
int l_s(int *a,int size,int item,int index)
{
if(index>size)
{
return (-1);
}
else
{
if(a[index]==item)
{
return index;
}
else
{
return l_s(a,size,item,index+1);
}
}
}
int main()
{
int *a,size,item,i,s;
printf("Enter the size of the array\n");
scanf("%d",&size);
a=(int*)malloc(size*sizeof(int));
printf("Enter the elements\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the search element\n");
scanf("%d",&item);
s=l_s(a,size,item,0);
if(s==-1)
{
printf("The search not succesfull\n");
}
else
{
printf("search succesful\n");
}
return 0;
}
Input:
Enter the size of the array
5
Enter the elements
3
4
8
2
1
Enter the search element
2
Output:
search succesful
No comments:
Post a Comment