w3resource logo


C Programming Exercises

C Exercises: Binary searching

Secondary Nav

C Recursion : Exercise-21 with Solution

Write a program in C for binary search using recursion.
Test Data :
Input the number of elements to store in the array :3
Input 3 numbers of elements in the array :
element - 0 : 25
element - 1 : 98
element - 2 : 18
Input the number to search : 18
Expected Output :

 The search number found in the array.

C Code:

#include <stdio.h>
 
int binarySearch(int*, int,int,int,int);

int main()
{

    int arr1[10],i,n,md,c,low,hg;
	
	printf("\n\n Recursion : Binary searching :\n");
	printf("-----------------------------------\n");

   printf(" Input the number of elements to store in the array :");
   scanf("%d",&n);
   
   printf(" Input %d numbers of elements in the array :\n",n);
   for(i=0;i<n;i++)
      {
	  printf(" element - %d : ",i);
	  scanf("%d",&arr1[i]);
	  }

    printf(" Input the number to search : ");
    scanf("%d",&md);

    low=0,hg=n-1;
    c=binarySearch(arr1,n,md,low,hg);
    if(c==0)
         printf(" The search number found in the array.\n\n");
    else
         printf(" The search number not exist in the array.\n\n");

    return 0;
 }

int binarySearch(int arr1[],int n,int md,int low,int hg)
{

     int mid,c=0;

     if(low<=hg)
     {
          mid=(low+hg)/2;
          if(md==arr1[mid])
          {
              c=1;
          }
          else if(md<arr1[mid])
          {
              return binarySearch(arr1,n,md,low,mid-1);
          }
          else
              return binarySearch(arr1,n,md,mid+1,hg);
     }
     else
       return c;
}

Flowchart :

Flowchart: Binary searching

C Practice online:

#include <stdio.h>
 
int binarySearch(int*, int,int,int,int);

int main()
{

    int arr1[10],i,n,md,c,low,hg;
	
	printf("\n\n Recursion : Binary searching :\n");
	printf("-----------------------------------\n");

   printf(" Input the number of elements to store in the array :");
   scanf("%d",&n);
   
   printf(" Input %d numbers of elements in the array :\n",n);
   for(i=0;i<n;i++)
      {
	  printf(" element - %d : ",i);
	  scanf("%d",&arr1[i]);
	  }

    printf(" Input the number to search : ");
    scanf("%d",&md);

    low=0,hg=n-1;
    c=binarySearch(arr1,n,md,low,hg);
    if(c==0)
         printf(" The search number found in the array.\n\n");
    else
         printf(" The search number not exist in the array.\n\n");

    return 0;
 }

int binarySearch(int arr1[],int n,int md,int low,int hg)
{

     int mid,c=0;

     if(low<=hg)
     {
          mid=(low+hg)/2;
          if(md==arr1[mid])
          {
              c=1;
          }
          else if(md<arr1[mid])
          {
              return binarySearch(arr1,n,md,low,mid-1);
          }
          else
              return binarySearch(arr1,n,md,mid+1,hg);
     }
     else
       return c;
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program