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
main()
{
………
……..
……..
recursive();
} /*main function end*/
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
factorial(int);
main()
{
fact,num;
printf("Enter A number to find the factorial");
scanf("%d",&num);
fact=factorial(num);
printf(fact);
0;
}
factorial(int n)
{
(n==0)
{
0;
}
(n==1)
{
1;
}
{
n*factorial(n-1);
}
}
|
Enter A number to find the factorial 5
factorial is 120
|
5*4*3*2*1=120