on 14 Oct 2017 11:31 AM

0 comments

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

The recursive call is placed after the exit condition and returns

For example, if you call the

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)**

**{**

Console.WriteLine(**Factorial(7)**);

//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.