Binary search program in C || Arrays || C programming
In this, we are going to a program in which we will be Searching a target element within sorted array using Binary Search method in C Programming Language.

//Searching a target element within sorted array using Binary Search method 

#include <stdio.h>

void main()
{
    int x[9] = {10,20,30,40,50,60,70,80,90};
    int target;
    int low,high,mid;
    printf("Enter any integer number: ");
    scanf("%d",&target);
    low = 0;
    high = 8;

    while(low<=high)
    {
        mid = (low+high)/2;
        if(target<x[mid])
        {
            high = mid -1;
        }
        else if(target>x[mid])
        {
            low = mid+1;
        }
        else
        {
            printf("Location of Target is: %d",mid+1);
            break;
        }
    }
}

#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post