Sie sind auf Seite 1von 4

PROGRAM-3

AIM:-WRITE A PROGRAM TO SEARCH AN ELEMENT IN THE


ARRAY USING BINARY SEARCH

#include<conio.h>
#include<stdio.h>
main()
{
int i,a[20],n,beg,end,mid,flag=0,item;
printf("\nenter the no of elements to store");
scanf("%d",&n);
printf("\nenter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nenter the element to find");
scanf("%d",&item);
beg=0;
end=n;
while(beg<=end&&a[mid]!=item)
{
mid=(beg+end)/2;
if(a[mid]==item)
{
printf("\nelement found at position %d",mid+1);
flag++;
}
else
{
if(item<a[mid])
end=mid-1;
else
beg=mid+1;
}
}
if(flag==0)
{
printf("\nelement not found");
}
getch();
}
OUTPUT:-

Das könnte Ihnen auch gefallen