-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQues 9.c
34 lines (32 loc) · 999 Bytes
/
Ques 9.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 9. Write a program to search an element in array using binary search
#include "stdio.h"
int main(int argc, char const *argv[]) {
int arr[20], n, i, s, low, high, mid, flag=0;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter %d elements(in ascending order):\n",n);
for(i=0; i<n; i++)
scanf("%d",&arr[i]);
printf("Enter the search element: ");
scanf("%d",&s);
low=0;
high=n-1;
while(low<=high)
{
mid = (low+high)/2;
if(s<arr[mid])
high = mid-1;
else if(s>arr[mid])
low = mid+1;
else
{
flag=1;
break;
}
}
if(flag==1)
printf("%d is at position %d",s,mid+1);
else
printf("%d not present in array",s);
return 0;
}