create a program using binary search in c or java that finds
create a program using binary search in c++ or java that finds the key and index of the array where the key was found. If key not found output it not in the array. The range of numbers is 1-100
Solution
//C++ code binary search
 #include <iostream>
using namespace std;
// binary search
 void binary_Search(int array[], int left, int right, int key)
 {
 while (left <= right)
 {
 int middle = left + (right-left)/2;
 
 // key is present at middle element
 if (array[middle] == key)
 {
 cout << key << \" found at index \" << middle << \"\ \ \";
 return;
 }
 
 // key is right side of middle element
 if (array[middle] < key)
 left = middle + 1;
 
 // key is left side of middle element
 else
 right = middle - 1;
 }
 
 // key not found
 cout << \"key is not present in the array\ \ \";
 }
 
 int main(void)
 {
 int array[] = {23, 43, 44, 55, 61, 67, 77, 86, 99, };
 int size = sizeof(array)/ sizeof(array[0]);
 int key;
cout << \"Array: \";
 for (int i = 0; i < size; ++i)
 {
 cout << array[i] << \" \";
 }
cout << endl << endl;
cout << \"Enter key value: \";
 cin >> key;
 binary_Search(array, 0, size-1, key);
 
 cout << \"Enter key value: \";
 cin >> key;
 binary_Search(array, 0, size-1, key);
 
 
 return 0;
 }
 /*
 output:
Array: 23 43 44 55 61 67 77 86 99
Enter key value: 70
 key is not present in the array
Enter key value: 55
 55 found at index 3
 */


