Recursion is a programming technique where a function calls itself directly or indirectly to solve a problem. Recursion involves breaking down a problem into smaller sub-problems, solving each sub-problem separately, and combining the solutions to obtain the final solution.
The basic idea of recursion is to reduce the problem to a simpler version of itself, and keep reducing it until it becomes small enough to be solved directly, without recursion. Recursion can be implemented in many programming languages, including C, C++, Java, Python, and more.
Here is an example of a recursive function in C that calculates the factorial of a given number:-
#include <stdio.h>
int factorial(int n)
{
if (n == 0)
{
return 1;
}
else {
return n * factorial(n – 1);
}
}
int main()
{
int n = 5;
int result = factorial(n);
printf(“Factorial of %d is %d\n”, n, result);
return 0;
}