c program :
#include<stdio.h>
int main()
{
int arr[' '], i, j, m, s, e, f, n, r = 0;
printf("enter the size of your array :");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("enter the %d number element", i);
scanf("%d", &arr[i]);
}
printf("enter the element you want to search ");
scanf("%d", &f);
s = 1;
e = n;
while (e >= s)
{
m = (s + e) / 2;
if (arr[m] == f)
{
r = m;
break;
}
if (arr[m] < f)
{
s = m + 1;
}
if (arr[m] > f)
{
e = m - 1;
}
else
r = 0;
}
if (r != 0)
printf("item found at %d", r);
else
printf("item not found");
}
Comments