Recursion

Recursion in function




Hi Everyone

Hi Family. Have a good day. Just keep smiling and enjoy your life. Stay safe from covid-19 and stay at home and learn something new in this time. So Keep smiling with smiles on your faces let's get started.

Today we are going to talk about recursion in function. So recursion is a kind of function where function calls itself. Means in function function call it self. There's re two types of recursion it is based on calling so how you call and how iteration is iterated on the bases the types are given. So in first type the goes to backside means 5,4,3,2,1 the ieration is iterated. And in second type the iteration iterated in faorward direction like 1,2,3,4,5. So these are the types of recursive function which is based on the type of iteration evaluation. So let's start the practicle session.

Practicle
#include <stdio.h>
int recur(int n)
{
    if(n < 2)
      return n;
      else
      return n*recur(n-1);
}
int main() {
int n;
printf("Enter number to find factorial using recursion: ");
scanf("%d",&n);
n=recur(n);
printf("Factorial on number is: %d",n);
return 0;
}
So this is an example of recursion here it is working like loop and we can use in place of recursion we can use loop or goto statement. Here the iteration is evaluated first 5*recur(5-1) then it is evaluated like 4*recur(4-1) then it is evaluated like 3*recur(3-1) after that 2*(2-1) as per the given condition if (n<2) it will return 1.

So now the value of recur(2-1) is 2 so 2*2means 4 it is the value of recur (3) so recur(4) will be 4*3 and the recur(4) will be 12 .

So now the iteration will be 2*recur(2-1) means 2*1
And then 3*recur(3-1) means 3*2=6 and the iteration 4*recur(4-1) will be 4*6 which is equals to 24 and then 5*recur(5-1) will be 5*24 which is equal to 120. So this is how the iteration is performed. So now if you want to learn the other type of recursion you can but it it too difficult to explain becuase in that type the iteration will be like 1*2*3*4*5 so to stop it at 5 is difficult and many online compiler will give you error so i am not going to explain you because it is not used too much but still if you want to learn it just mail me i will explain it to you and we both will do the practicle simultaneously.

So thank you for watching this blog. In the next blog we will talk about the goto statement and about the loops. So stay tuned for the next blog.

Thanks to all of you for watching this blog. Thanks to my entire family of more than 600 viewers thanks to all of them and thanks to all of you for growing our blog. With you all we have achived more than 600 viewers in 4 days it is a big achievement so thanks to all of you.


Comments