Everything you want to know about Recursion?

What is recursion

Why Use Recursion

Difference between Recursion and Iteration

How to Use Recursion

Every recursive program follows the same basic sequence of steps:

Some Examples

void print_number(int n)
{
unsigned int x = n;
if (n < 0)
{
putchar('-');
x = -x;
}
if ((x / 10) > 0)
{
print_number(x / 10);
}
putchar(x % 10 + '0');
}
int factorial(int n)
{
int x;
if (n == 0)
{
return (1);
}
else if (n < 0)
{
return (-1);
}
x = n * factorial(n - 1);
return (x);
}

int factorial(int n)
{
int i;
unsigned long long fact = 1;
// shows error if the user enters a negative integer
if (n < 0)
return (-1);
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
return fact;
}
float _pow_recursion(float x, float y)
{
if (y == 0)
return (1);
if (y < 0)
return (_pow_recursion(x, y + 1) / x);

return (_pow_recursion(x, y - 1) * x);
}
void print_binary(unsigned long int n)
{
if (n > 1)
print_binary(n >> 1);
putchar((n & 1) + '0');
}

Stack With Recursion

What are the disadvantages of recursive programming over iterative programming?

What are the advantages of recursive programming over iterative programming?

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store