Welcome to our BMR AI CHATBOT.
A free expermental AI tool where you can interact with the webpage, ask question about the webpage and other related doubts.
In some cases reponses may take time to get. In case of error give us your report.
You responses are stored for experimental purpuses. And your personal info is not integrated with you in any way.
Note: AI can make mistakes and can give in appropiate responses. Your feedbak will help us improve.
Stay tuned for more AI products and tools
And Finally don't forget to give your feedback. click on the icon provided to give feedback.
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.
#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 - Binary Search.
#include <stdio.h>
int binarySearch(int arr[], int low, int high, int target) {
if (low > high) {
return -1; // Base case: element not found
}
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
}
}
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 compiler.