Tuesday, 26 September 2017

How to solve Factorial problem using recursion?

Here factorial is a function which takes the value as an argument, for which we have to find the factorial. If the value is less than or equal to zero (0) then factorial will one (1). Below are the steps which are taken to find the factorial of any given value. 

// definition of factorial function which takes value as argument.

int factorial(int value)
{
    if(value <= 0)
        return 1;                            
   else
{
        return value * factorial(value-1); 

}

//Step for solution of factorial function: Here value given to the factorial function is  5.

1.  if condition is not operated in the factorial function because value is 5 for first time.
2. else condition performed and it return 5 * (factorial(4));
3. else condition performed and it return 5 * (4 * factorial(3));
4. else condition performed and it return 5 * (4 * (3 * factorial(2)));
5. else condition performed and it return 5 * (4 * (3 * (2 * factorial(1))));
6. else condition performed and it return 5 * (4 * (3 * (2 * (1)))); i.e 120.



Thanks 
Mukesh Rajput

No comments:

Post a Comment

Thanks
Mukesh Rajput