Saturday, 16 November 2013

Linear search

Linear Search With Recursion Using C program


Program:

#include<stdio.h>

void main()

{

   int n,a[20],i,j,key;

   clrscr();

   printf("enter no of elements to be entered");

   scanf("%d",&n);

   printf("enter elements");

   for(i=1;i<=n;i++)

      scanf("%d",&a[i]);

   printf("enter the key to search");

   scanf("%d",&key);

   j=rlsearch(a,1,n,key);

   if(j==0)

       printf("the element is not found");

   else

       printf("the element found at %d",j);

   getch();

}

int rlsearch(int a[],int i,int n,int key)

{

   if(i>n)

      return 0;

   if(a[i]==key)

      return i;

   else 

      return rlsearch(a,i+1,n,key);

}

Output:




No comments:

Post a Comment