Binary searching in C-language

Program to search an element using binary search.

#include<stdio.h>
int main()
{
    int i,a[10],n,b,c=0,l,f,mid;
    printf("Enter the size limit maximum is 10\n");
    scanf("%d",&n);
    printf("Enter the array element\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    printf("Enter the element to be searched\n");
    scanf("%d",&b);
    f=0;
    l=n-1;
    while(f<=l)
    {
        mid=(f+l)/2;
        if(b<a[mid])
        {
            l=mid-1;
        }
        if(b>a[mid])
        {
            f=mid+1;
        }
        else if(b==a[mid])
        {
            c=1;
            break;
        }
    }
    if(c==0)
    {
        printf("Element not found");
    }
    else
    {
        printf("Element found at %d position",mid+1);
    }
    }

OUTPUT
Enter the size limit maximum is 10
4
Enter array element
4
6
3
9
Enter the element to be searched
6
Element found at 2 position

Comments

Popular posts

Creative pattern of butterfly in C-language

Program to show present time and date in C-language

Introduction of C-language.

Program to cheque enter number is a perfect number or not using if else in C-language

Program to cheque four digits number is palindrome or not in C-language

Program to print the cube of a number in c-language

Simple Hello world program in c- language