BMR AI
By using this service you agree to read and accept our Terms and conditions and Privacy Policy
Chat Now
GUIDE

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.

REPORT ERROR

SUBMIT
DELETE CHAT

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 - Binary Search.


Example

#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
    }
}

Quick Recap - Topics Covered

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 compiler.

Example JS

Example 1 Example 2 Example 3 Example 4 Example 5

Quiz


FOLLOW FOR MORE UPDATES

Official Facebook Account Official Instagram Account Offcial X (formerly known as Twitter) Account Offficial Youtube Channel
BMR EDUCATION © 2024 All rights reserved