Pages

Write A Program Search In Given Number In Array Using Binary Search

Wednesday, 8 May 2013

#include <stdio.h>
#include <conio.h>
int main()
{
   int c, f, l, midl, n, s, array[100];

   printf("Enter number of elements: ");
   scanf("%d",&n);

   printf("Enter %d integers: ", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d",&array[c]);

   printf("Enter value to find: ");
   scanf("%d",&s);

   f = 0;
   l = n - 1;
   midl = (f+l)/2;

   while( f <= l )
   {
      if ( array[midl] < s )
         f = midl + 1;
      else if ( array[midl] == s )
      {
         printf("%d found at location %d.", s, midl+1);
         break;
      }
      else
         l = midl - 1;

      midl = (f + l)/2;
   }
   if ( f > l )
      printf("Not found! %d is not present in the list.", s);

   getch();
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *