0% found this document useful (0 votes)
11 views1 page

Int Int: "Enter Number of Elementsn" "%D" "Enter %D Integersn" "%D" "Enter Value To Findn" "%D"

The document contains code for a binary search algorithm to search for a key value in a sorted array. The code takes input of array size and elements, key to search for, and uses a while loop to repeatedly check the middle element and update the low and high indexes until the key is found or not found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views1 page

Int Int: "Enter Number of Elementsn" "%D" "Enter %D Integersn" "%D" "Enter Value To Findn" "%D"

The document contains code for a binary search algorithm to search for a key value in a sorted array. The code takes input of array size and elements, key to search for, and uses a while loop to repeatedly check the middle element and update the low and high indexes until the key is found or not found.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>
int main()
{
int i, low, high, mid, n, key, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for(i = 0; i < n; i++)
scanf("%d",&array[i]);
printf("Enter value to findn");
scanf("%d", &key);
low = 0;
high = n - 1;
mid = (low+high)/2;
while (low <= high) {
if(array[mid] < key)
low = mid + 1;
else if (array[mid] == key) {
printf("%d found at location %d.n", key, mid+1);
break;
}
else
high = mid - 1;
mid = (low + high)/2;
}
if(low > high)
printf("Not found! %d isn't present in the list.n", key);
return 0;

You might also like