#include<stdio.h>//header file
#include<process.h>//header file
#include<conio.h>//header file
int s;
void bin(int a[],int,int);
void main() //starting of the main() function
{
int l,u,i,a[10]; //declaration of the variables
clrscr();
printf("program to search an element using binary search");
printf("\n\n\n\t\t------------INPUT-------------");
l=0;
u=9;
printf("\n\nenter the elements in sorted form");
for(i=0;i<10;i++) //taking input from the user
{
printf("\nenter the %dth number",i);
scanf("%d",&a[i]);
}
printf("\n\nenter a element that has to be searched");
scanf("%d",&s);
bin(a,l,u);
getch();
}
void bin(int a[],int l,int u)
{
int m;
m=(l+u)/2;
if(s<a[m])
{
if(u>=l)
{
u=m-1;
bin(a,l,u);
}
else
{
printf("\n\n\n\t\t------------OUTPUT------------");
printf("\n\nno. is not present in the list");//printing output
getch();
exit(0);
}
}
else if(s>a[m])
{
if(l<=u)
{
l=m+1;
bin(a,l,u);
}
else
{
printf("\n\n\n\t\t------------OUTPUT------------");
printf("\n\nthe no. is not present in the list");//printing output
getch();
exit(0);
}
}
else if(s==a[m])
{
printf("\n\n\n\t\t------------OUTPUT------------");
printf("\n\nelement exists at %d",m);//printing output
getch();
exit(0);
}
}