// Program that uses a recursive function search that checks whether q is present in the // array. If q is present, it returns the index where q occurs in the array; // otherwise, it returns -1. // // Author:rahule@cse.iitk.ac.in #include int search(int a[],int,int,int); main() { int a[10],t,i=0,n,op,q; printf("\nEnter the numbers..\n"); for(i=0;i<10;i++) //scans every input till a negative input is encounterd { scanf("%d",&t); if(t<0) break; a[i]=t; } n=i; //calculates total number of elements printf("\nEnter the element to be searched :"); scanf("%d",&q); op=search(a,q,0,n); if(op==-1) printf("\nthe number %d is not in the given array\n",q); else printf("\nthe number %d is found at index %d in the given array!\n",q,op); } int search(int a[],int q,int i,int n) { if(i==n) return(-1); if(a[i]==q) return(i); else return(search(a,q,i+1,n)); }