# C Recursion

It's a programming concept in which a function calls itself directly or indirectly to solve a problem.
It's a powerful and elegant technique, especially for solving problems that can be broken down into smaller, similar sub-problems.

### Example

``````#include <stdio.h>

// Recursive function to calculate factorial
int factorial(int n) {
// Base case: factorial of 0 or 1 is 1
if (n == 0 || n == 1) {
return 1;
} else {
// Recursive case: n! = n * (n-1)!
return n * factorial(n - 1);
}
}

int main() {
int result;

// Function call
result = factorial(5);

// Output the result
printf("Factorial: %d\n", result);

return 0;
}
``````

The code example: The factorial function calls itself with a smaller argument until it reaches the base case (n == 0 or n == 1), at which point it returns 1.
The intermediate results are multiplied to calculate the final factorial.
Try another example

### Example - Binary Search

``````#include <stdio.h>

int binarySearch(int arr[], int low, int high, int target) {
if (low > high) {
}

int mid = (low + high) / 2;

if (arr[mid] == target) {
return mid; // Base case: element found
} else if (arr[mid] > target) {
return binarySearch(arr, low, mid - 1, target); // Recursive case: search in the left half
} else {
return binarySearch(arr, mid + 1, high, target); // Recursive case: search in the right half
}
}``````

# Quick Recap

C Recursion

## Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compile

### Example 1

Example 1 Example 2 Example 3 Example 4 Example 5