Recursion in c

Recursion in c

The recursion is the process where problem is defined itself. so in the recursion the problem is solve by repeatedly breaking it into smaller problems, or recursion is a process of repeating item in a self-similar way in the programming any function call inside the same function.To implement recursion technique in a programming, a function should be capable of calling itself or A recursive function is a function which calls itself.

To understand the recursion let us take example

int main()
{
………
……..
……..

recursive();
} /*main function end*/
void recursive()
{
………..
………..

recursive(); /* recursive call */
} /* end of recursive function*/

in this example the recursive() is calling inside its own function body, so here recursive() is recursive function. In the first when main function execute this main call the recursive(), the code of recursive() executed but inside the recursive function again call recursive or itself. Again recursive() executes so its means this process will go in infinitely, but for termination. condition can give to terminate the function. also know as exit condition.

.

To understand the factorial take the example in this example find factorial using recursion function

#include<stdio.h>
int
factorial(int);
int main()
{
int fact,num;
printf("Enter A number to find the factorial");
scanf("%d",&num);
fact=factorial(num);
printf("Factorial is %d",fact);
return 0;
}
int factorial(int n)
{
if(n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
return n*factorial(n-1);
}
}

Enter A number to find the factorial 5 factorial is 120

factorial in c

5*4*3*2*1=120