by Dinesh Thakur Category: Array
#include <stdio.h>

#include <conio.h>
main()
{
    int arr[10], num, i, n , pos =-1, beg, end,mid, found =0;
    clrscr ();
    printf("\n Enter the number of elements in the array: ");
    scanf ("%d", &n);
    printf (" \n Enter the elements: ");
  for(i=0;i<n;i++)
  {
      scanf("%d", &arr[i]);
  }
      printf("\n Enter the number that has to be searched: " );
      scanf ("%d", &num);
      beg = 0, end = n-1;
   while (beg <end)
   {
      mid= (beg+ end)/2;
       if (arr[mid] == num)
       {
         printf("\n %dis present in the array at position = %d", num, mid);
         found=1;
         break;
       }
            if (arr[mid]>num)
            {
               end = mid-1;
            }
         else if (arr[mid] < num)
         beg = mid+1;
   }
      if ( beg > end &&found == 0)
      {
         printf("\n %d DOESNOTEXIST IN THE ARRAY",num);
      }
          getch();
          return 0;
}

implement binary search



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles