Recursion In C#

Hello Techies,

Today, in this blog we are going to learn the concept of the Recursion by just solving a simple example which is popular in coding, which is finding the factorial.

So, here the concept of Recursion is simply the function that is calling itself based on some condition. So, let's jump into it and start to learn how we can implement it. 

Before starting to implement we require some things: function to make code reusable or for the recursion and integer variable which stores the value from the user for which we have to find the factorial.

So, let's just get the value from the user, check out the following snippet.

// Console returning the value in String so Require to Convert or parse to integer....
int n = Convert.ToInt32(Console.ReadLine().Trim());

Now, we got the value in "n" of which we have to find the factorial. So, now firstly check the concept of the Recursion with an equation to find the factorial.

public static int factorial(int n)
     if (n == 1) return 1;
     return factorial(n - 1) * n; //Recursion...

Now, here the above code snippet is performing the multiplication with its previous number and then returning the value and also performing the decrement on the number till the number is greater than or equal to 1. And if the number is 1 then it will return 1 as its factorial.

Let's compile the code; click on the link below,

Check out the following full Source Code.

This contains the solution for the HackerRank 30DaysofCode Day 9 Recursion 3.

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
class Result {
     * Complete the 'factorial' function below.
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER n as parameter.
    public static int factorial(int n) {
        if (n == 1) return 1;
        return factorial(n - 1) * n;
class Solution {
    public static void Main(string[] args) {
        int n = Convert.ToInt32(Console.ReadLine().Trim());