top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C: Program for binary search using recursion?

+2 votes
349 views
C: Program for binary search using recursion?
posted Apr 28, 2015 by Jalal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Program for Binary Search:

#include<stdio.h>
int main(){

    int a[10],i,n,m,c,l,u;

    printf("Enter the size of an array: ");
    scanf("%d",&n);

    printf("Enter the elements of the array: " );
    for(i=0;i<n;i++){
         scanf("%d",&a[i]);
    }    
    printf("Enter the number to be search: ");
    scanf("%d",&m);

    l=0,u=n-1;
    c=binary(a,n,m,l,u);
    if(c==0)
         printf("Number is not found.");
    else
         printf("Number is found.");    
    return 0;
 }

int binary(int a[],int n,int m,int l,int u){

     int mid,c=0;

     if(l<=u){
          mid=(l+u)/2;
          if(m==a[mid]){
              c=1;
          }
          else if(m<a[mid]){
              return binary(a,n,m,l,mid-1);
          }
          else
              return binary(a,n,m,mid+1,u);
     }
     else
       return c;
}

Output:

Enter the size of an array: 5

Enter the elements of the array: 8 9 10 11 12

Enter the number to be search: 8

Number is found.

answer Apr 29, 2015 by Mohammed Hussain
...