Binary Search use Recursion in C or C Program for Binary Search using Array Passing UDF

Binary Search using Recursion function in C or C Program for Binary Search using Array Passing User Defined Function :-

Example : How to write C Program for Binary Search using Recursive Function and its solutions given below :-

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

int binarySearch(int lmt[],int arrayStart,int arrayEnd,int a) /* UDF declar with definition */
{
int m,pos;
if (arrayStart<=arrayEnd)
{
m=(arrayStart+arrayEnd)/2;
if (lmt[m]==a)
return m;
else if ( a < lmt[m] )
return binarySearch(lmt,arrayStart,m-1,a); /* function calls itself called Recursive function */
else
return binarySearch(lmt,m+1,arrayEnd,a); /* function calls itself called Recursive function */
}
return -1;
}

void readArray(int lmt[],int n) /* UDF declar with definition */
{
int i;
printf("Enter the array elements :\n");
for( i=0 ; i < n ; i++ )
scanf("%d",&lmt[i]);
}

/* main() function starts here */
void main()
{

int lmt[10], num, searchElement,pos;

clrscr();


printf("Enter the number of array elements to be printed :");
scanf("%d",&num);

readArray(lmt,num);

printf("\nEnter the element you want to search :");
scanf("%d",&searchElement);

pos=binarySearch(lmt,0,num,searchElement);
if(pos==-1)
{
printf("\nElement not found");
}
else
{
printf("\nElement found at %d position",++pos);
}

getch();
}
/*
if we give no of elements is 5 and array elements are 11,22,33,66,45 then element to be searched is 66 then answer will be "Element found at 4 position" otherwise "Element not found"
*/