Factorial of a number is calculated by multiplying all the numbers from 1 till the number itself. For any given input number, we are required to calculate factorial, using recursion. Recursion is a technique in which a method calls itself in a loop until some breaking statement is met. This technique makes programming significantly simple and is used in complex computations to save memory.

```
public static double ComputeFactorial(int n)
{
if (n >= 1)
{
var compute = n * ComputeFactorial(n - 1);
return compute;
}
else
return 1;
}
```

**How it works:**

Assume we're passing a number 5 to the function, it then checks if the number is not less than 1. Since 5 isn't less than 1, the method returns product of the number passed (in this case 5) along with a call to itself passing number decreased by 1. And this continues till the number becomes 0, which is a breaker and then the total computation is returned.

```
******* Factorial Recursion *******
Enter input:5
1 calling out on 0 returns 1
2 calling out on 1 returns 2
3 calling out on 2 returns 6
4 calling out on 3 returns 24
5 calling out on 4 returns 120
5! = 120
```

Enjoy this article?
*Buy Me a Coffee*

Join the community! Like on Facebook Follow on Twitter

Compute and generate a compressed string for a given string containing repetitions

Find all possible natural numbers below a given limit such that a3+b3 = c3+d3

Find all the pairs in a given ordered set of numbers whose sum is equal to a given input sum

Find all the pairs in a given unordered set of numbers whose sum is equal to a given input sum

Sort the given unordered set of Binary digits

(UPDATED to .NET 6) Implementing CQRS with MediatR in ASP.NET Core

(UPDATED to .NET 6) Calling Stored Procedures in ASP.NET Core / EF Core

(UPDATED to .NET 5) Integrating OData with ASP.NET Core Web API

Integrating MongoDB with ASP.NET Core (.NET 6)

Implementing Role based and Claims based Authorization in ASP.NET Core (.NET 5)