The document contains a student's name, UID, branch, semester, and response to a question asking for a C program to implement binary search. The student provides a binarySearch function that takes an array, left and right indices, and a target value. It recursively searches the sorted array by checking the middle element and updating the left or right index based on whether the middle element is less than or greater than the target. The main function calls binarySearch on a sample array, prints whether the target element is found or not found based on the return value.
The document contains a student's name, UID, branch, semester, and response to a question asking for a C program to implement binary search. The student provides a binarySearch function that takes an array, left and right indices, and a target value. It recursively searches the sorted array by checking the middle element and updating the left or right index based on whether the middle element is less than or greater than the target. The main function calls binarySearch on a sample array, prints whether the target element is found or not found based on the return value.
Ans1- #include<stdio.h> int binarySearch(int arr[], int l, int r, int x) { while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; A } return -1; } int main(void) { int arr[] = { 2, 3, 4, 10, 40 }; int n = sizeof(arr) / sizeof(arr[0]); int x = 40; int result = binarySearch(arr, 0, n - 1, x); (result == -1) ? printf("The element is not present" " in array") : printf("The element is present at " "index %d", result); return 0; } OUTPUT: