#include <stdio.h>
#include <conio.h>
void main()
{
int a[5];
int mid, lower=0, upper=4, flag=1, num, i;
clrscr();
printf("\n Enter the array elements: ");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
while(lower <= upper)
{
mid=(lower+upper)/2;
if(a[mid]==num)
{
printf("\n The no is present at position %d",mid);
flag=0;
break;
}
else
{
if(a[mid] > num)
upper=mid-1;
else
lower=mid+1;
}
}
if(flag)
printf("The no is not present in the array");
getch();
}
#include <conio.h>
void main()
{
int a[5];
int mid, lower=0, upper=4, flag=1, num, i;
clrscr();
printf("\n Enter the array elements: ");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
while(lower <= upper)
{
mid=(lower+upper)/2;
if(a[mid]==num)
{
printf("\n The no is present at position %d",mid);
flag=0;
break;
}
else
{
if(a[mid] > num)
upper=mid-1;
else
lower=mid+1;
}
}
if(flag)
printf("The no is not present in the array");
getch();
}
No comments:
Post a Comment