#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,k,mid,s,e,f=0;
clrscr();
printf("\n Enter how many number u want:-");
scanf("%d",&n);
printf("\n Please enter %d sorted numbers:-",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n numbers are:-");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
printf("\n\n Enter search key number:-\n");
scanf("%d",&k);
s=0;
e=n-1;
while(s<=e)
{
mid=(s+e)/2;
if(k==a[mid])
{
printf("\n %d number is found on %d position.",k,mid+1);
f=1;
break;
}
else
{
if(k>a[mid])
{
s=mid+1;
}
else
{
e=mid-1;
}
}
}
if(f==0)
{
printf("\n number is not found");
}
getch();
}
/* OUTPUT
Enter how many number u want:-5
Please enter 5 sorted numbers:-10 11 12 13 14
numbers are:- 10 11 12 13 14
Enter search key number:-
12
12 number is found on 3 position. */
#include<conio.h>
void main()
{
int a[20],n,i,k,mid,s,e,f=0;
clrscr();
printf("\n Enter how many number u want:-");
scanf("%d",&n);
printf("\n Please enter %d sorted numbers:-",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n numbers are:-");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
printf("\n\n Enter search key number:-\n");
scanf("%d",&k);
s=0;
e=n-1;
while(s<=e)
{
mid=(s+e)/2;
if(k==a[mid])
{
printf("\n %d number is found on %d position.",k,mid+1);
f=1;
break;
}
else
{
if(k>a[mid])
{
s=mid+1;
}
else
{
e=mid-1;
}
}
}
if(f==0)
{
printf("\n number is not found");
}
getch();
}
/* OUTPUT
Enter how many number u want:-5
Please enter 5 sorted numbers:-10 11 12 13 14
numbers are:- 10 11 12 13 14
Enter search key number:-
12
12 number is found on 3 position. */