Write a program to search a number in sorted data set

/*
PROGRAM:
——–
Write a program to search a number in sorted data set.
*/
#include<stdio.h>
#include<conio.h>

void main()
{
int a[10];
int lb=0,ub=9,mid,i,num;
clrscr();

for(i=0;i<10;i++)
{
printf(“\nEnter Number-%d: “,i+1);
scanf(“%d”,&a[i]);
}
printf(“\n\nEnter the number to be found: “);
scanf(“%d”,&num);

while(lb<=ub)
{
mid=(lb+ub)/2;
if(a[mid]==num)
{
printf(“\n\nNumber found.”);
break;
}
else
{
if(a[mid]>num)
ub=mid-1;
else
lb=mid+1;
}

}
if(lb>ub)
printf(“\n\nNumber not found.”);

getch();
}
/*
OUTPUT:
——-
Enter Number-1: 1

Enter Number-2: 2

Enter Number-3: 3

Enter Number-4: 4

Enter Number-5: 5

Enter Number-6: 6

Enter Number-7: 7

Enter Number-8: 8

Enter Number-9: 9

Enter Number-10: 10
Enter the number to be found: 7
Number found.
*/

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.