Below we will use of function recursion with an example.
Write a simple example to print “welcome to gyantoday.com” 5 times.
#include<stdio.h>
void func(int count)
{
while (count < 5)
{
printf(“welcome to gyantoday.com\n”);
count++;
}
}
int main()
{
int count = 0;
func();
return 0;
}
Now we will write a program for same output using recursive function.
#include<stdio.h>
void func(int count)
{
if (count < 5)
func(count+1);
printf(“welcome to gyantoday.com\n”);
}
int main()
{
int count = 0;
func();
return 0;
}
We will see extensive use of function recursion in DATA STRUCTURE, meanwhile giving below some example of use of function recursion.
- Find the factorial of a given number using recursion.
- Fibonacci series using recursion.
- Write a C program for palindrome using recursion.
- Reverse a string using recursion.
- Find the power of a number using recursion.