By Team Escalera
on 14 Oct 2017 11:31 AM
  • Recursion

A method that calls itself is called a recursive method.

Calculating the factorial of a number is One of the tasks that can be solved using recursion.
In Maths,  Factorial of a number x is denoted as x! and it is :
For example:5! = 5*4 * 3 * 2 * 1 = 120

It concludes that the factorial of any number can be calculated using number x number-1 and such that : number-1 reaches “1”.
We can define our method based on the above conclusion and statement :

static int Factorial(int number) {
if (number == 1) {
return 1;
return number * Factorial(number – 1);

In the Factorial which is a recursive method, the if statement defines the exit condition, it is a base case that requires no recursion. In this case, when number becomes equal to one, the solution is simply to return 1 (because : the factorial of one is one).
The recursive call is placed after the exit condition and returns number multiplied by the factorial of n-1.
For example, if you call the Factorial method with the argument 5, it will execute as follows:
return 5*Fact(4), which is 5*4*Fact(3), which is 5*4*3*2*Fact(1), which is finally 5*4*3*2*1.


Our Factorial can be called from Main:

static void Main(string[] args)
//Output will be :  “5040”

Here is a small Quiz for Students :

Which of the below given option prevents the recursive method to call itself forever?


A. the Main method

B. the static keyword

C. the exit condition

Answer in comments section and get access to all tutorials and articles.

Comments (0)

Leave A Comment