BINARY SEARCH USING C PROGRAM

BINARY SEARCH USING C PROGRAM


Solution:

#include<stdio.h>
int main(){
  int a[10],i,n,m,c=0,l,u,mid;
  printf("Enter the size of an array->");
  scanf("%d",&n);
  printf("\nEnter the elements of the array->");
  for(i=0;i<n;i++){
      scanf("%d",&a[i]);
  }
  printf("\nThe elements of an array are->");
  for(i=0;i<n;i++){
      printf(" %d",a[i]);
  }
  printf("\nEnter the number to be search->");
  scanf("%d",&m);
  l=0,u=n-1;
  while(l<=u){
      mid=(l+u)/2;
      if(m==a[mid]){
      c=1;
          break;
      }
      else if(m<a[mid]){
      u=mid-1;
      }
      else
      l=mid+1;
  }
  if(c==0)
      printf("\nThe number is not in the list");
  else
      printf("\nThe number is found");
  return 0;
}

More C Program :


  1. BINARY SEARCH USING C PROGRAM
  2. write a c program for binary search
  3. program to search an element using binary search
  4. linear search program using c
  5. binary search program in c language
  6. binary search algorithm
  7. c program for binary search using recursion
  8. c program for binary search using function
  9. c program for binary search using iteration


Learn More :