#include<stdio.h>
main()
{
int n,i,a[20],key;
printf("Enter n:");
scanf("%d",&n);
printf("Enter elements into array:\n");
for(i=0; i<n; i++)
{ scanf("%d",&a[i]);
}
printf("Enter element to be searched\n");
scanf("%d",&key);
int start=0,end=n-1,mid;
while(start<=end)
{
mid=(start+end)/2;
if(a[mid]==key)
{
printf("%d found at %d",key,mid+1);
break;
}
else if(key<a[mid])
{
end=mid-1;
}
else
{
start=mid+1;
}
}
if(start>end)
{
printf("Element not found!!");
}
}
Note: Need to be arranged in compiler after copied
OutPut:
Enter n:5
Enter elements into array:
2
4
6
8
9
Enter element to be searched
4
4 found at 2