Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- int low,high,mid,j,n,x;
- int a[10];
- printf("Enter how many numbers");
- scanf("%d",&n);
- for(j=0;j<n;j++)
- {
- scanf("%d",&a[j]);
- }
- printf("which number do you want");
- scanf("%d",&x);
- low=1;
- high=n;
- while(low<=high)
- {
- mid=(low+high/2);
- if(x<a[mid])
- {
- high=mid-1;
- }
- else if(x>a[mid])
- {
- low=mid+1;
- }
- else if(a[mid]==x)
- {
- printf("number found %d",mid);
- j=1;
- break;
- }
- }
- if(low>high)
- {
- printf("number not found");
- }
- return mid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement