Recursion is an important programming technique that causes a function to call itself. One example is the calculation of factorials. The factorial of 0 is defined specifically to be 1. The factorial of n, an integer greater than 0, is the product of all the integers in the range from 1 to n.
For example:
4! = 4 * 3 * 2 * 1 = 24
As you can see, a factorial can be thought of as repeatedly calculating num * num-1 until you reach 1.
Based on this solution, let’s define our method:
static int Fact(int num) {
if (num == 1) {
return 1;
}
return num * Fact(num – 1);
}
In the Fact recursive method, the if statement defines the exit condition, a base case that requires no recursion. In this case, when num equals one, the solution is simply to return 1 (the factorial of one is one).
The recursive call is placed after the exit condition and returns num multiplied by the factorial of n-1.
For example, if you call the Fact method with the argument 4, it will execute as follows:
return 4*Fact(3), which is 4*3*Fact(2), which is 4*3*2*Fact(1), which is 4*3*2*1.
Now we can call our Fact method from Main:
The Code:

The Output of Your Program:

The factorial method calls itself, and then continues to do so, until the argument equals 1. The exit condition prevents the method from calling itself indefinitely.
