Write a program for Binary search.
#include <stdio.h>
#include <conio.h>
void main ( )
{
int n[] = {12, 45, 50, 55, 60, 65, 70, 75, 80, 90, 99, 100};
int i,no, b = 0, l =11, m, flag = 1;
clrscr ( );
printf ("\n\n\t List of Numbers =");
for ( i = 0; i <= 11; i++ )
{
printf (" %d", n[i]);
}
printf ("\n\n\n\t\t\t Enter a number...........:"); scanf ("%d", &no);
while ( b <= l )
{
m = ( b + l ) / 2;
if ( n[m] == no )
{
flag = 0;
break;
}
if ( n[m] < no )
{
b = m + 1;
}
else
{
l = m - 1;
}
}
if ( flag == 0 )
printf ("\n\n\t\t\t SUCCESFUL");
else
printf ("\n\n\t\t\t UNSUCCESFUL");
getch ( );
}
Click here to contact us
No comments:
Post a Comment