//Program to search an element in array using Binary search in C++.
#include<iostream.h>
#include<iostream.h>
#include<conio.h>
void main()
{
int i, beg, end, loc, mid, item, data[50], n;
clrscr();
cout<<"Enter the number of elements in the array:";
cin>>n;
cout<<endl<<" Enter the value of "<<n<<" elements:";
for(i=0; i<n; i++)
{
cin>>data[i];
}
beg=0;
end=n-1;
mid=int((beg+end)/2);
cout<<endl<<" Enter the given element";
cin>>item;
while(beg<=end && data[mid]!=item)
{
if(item<data[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=int((beg+end)/2);
}
if(data[mid]==item)
{
loc=mid;
cout<<endl<<"Element found at location:"<<loc;
}
else
{
cout<<endl<<"Element is not found";
}
getch();
}