Binary Searching using Iteration Method – Algorithms & Data Structures in C++

//BINARY SEARCH USING ITERATION
#include<iostream>
using namespace std;
int binarysearch(int a[],int n,int x){
int start=0;
int end=n-1;
while(start<=end){

int mid=(start+end)/2;
if(x==a[mid])

return mid;
else if(x<a[mid])

end=mid-1;
else
start=mid+1;
}
return -1;
}
int main(){
int a[]={4,6,8,10,12,14,18,20};
cout<<“enter a number:\n”;
int x;
cin>>x;
int index=binarysearch(a,8,x);
if(index!=-1)

cout<<“\n number is at index “<<index;
else

cout<<“\n number not found”<<x;

}