Recursive Functions
#include <stdio.h>
int binarySearch(int arr[], int low, int high, int target) {
if (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == target)
return mid;
if (arr[mid] > target)
return binarySearch(arr, low, mid - 1, target);
return binarySearch(arr, mid + 1, high, target);
}
return -1;
}
void towerOfHanoi(int n, char source, char auxiliary, char destination) {
if (n == 1) {
printf("Move disk 1 from %c to %c\n", source, destination);
return;
}
towerOfHanoi(n - 1, source, destination, auxiliary);
printf("Move disk %d from %c to %c\n", n, source, destination);
towerOfHanoi(n - 1, auxiliary, source, destination);
}
int main() {
int sortedArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int target = 6;
int result = binarySearch(sortedArray, 0, sizeof(sortedArray) / sizeof(sortedArray[0]) - 1, target);
if (result != -1) {
printf("Binary Search: Element %d found at index %d\n", target, result);
} else {
printf("Binary Search: Element %d not found in the array\n", target);
}
int numberOfDisks = 3;
printf("\\nTower of Hanoi:\\n");
towerOfHanoi(numberOfDisks, 'A', 'B', 'C');
return 0;
}