CPP Program For Binary Search

A user is asked to enter 5 elements for searching. Then it will ask you to search that particular number. And it will display its position if the number present and if the number is not present then it will display not found.

#include<iostream.h>
#include<conio.h>
 
void main()
{
clrscr();
int a[5],n,i;
int k=0;

cout << "enter the array elements" << endl;  //input
for(i=0;i<5;i++)
{
cin >> a[i];
}

 
cout << "the array data are" << endl;      //display
for(i=0;i<5;i++)
{
cout << a[i] << endl;
}

cout << "enter the number you want to search" << endl;
cin >>n;

for(i=0;i<5;i++)
{
if(a[i]==n)
{
cout << "the array element is available found you entered at index " << i << endl;
}

else
{
k=k+1;
}
}

if(k>4)
{
cout << " not found" << endl;
}
getch();
}

 

Leave a Reply