---
title: "Create a function to calculate the factorial of a given number using recursion."  
description: "Create a function to calculate the factorial of a given number using recursion."  
author: "Utpal Vishwas"  
published: 2023-06-14  
updated: 2023-06-15  
canonical: https://www.mindstick.com/forum/158751/create-a-function-to-calculate-the-factorial-of-a-given-number-using-recursion  
category: "c#"  
tags: ["c#", "programs"]  
reading_time: 1 minute  

---

# Create a function to calculate the factorial of a given number using recursion.

Create a [function to calculate](https://www.mindstick.com/forum/158909/how-do-you-use-the-avg-function-to-calculate-the-average-value-of-a-column-in-sql) the factorial of a given number using recursion.

## Replies

### Reply by Aryan Kumar

Sure, here is the C# code to create a [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to calculate the factorial of a given number using recursion:

C#

```plaintext
using System;

public class Factorial
{
    public static int Factorial(int number)
    {
        if (number == 0)
        {
            return 1;
        }
        else
        {
            return number * Factorial(number - 1);
        }
    }

    public static void Main(string[] args)
    {
        int number = Convert.ToInt32(Console.ReadLine());
        int factorial = Factorial(number);
        Console.WriteLine("{0}! = {1}", number, factorial);
    }
}
```

This function first checks if the given number is equal to 0. If it is, then the function returns 1. Otherwise, the function returns the product of the given number and the factorial of the number minus 1. The Main() method then reads a number from the user and calls the Factorial() function to calculate the factorial of the number. The result is then printed to the console.


---

Original Source: https://www.mindstick.com/forum/158751/create-a-function-to-calculate-the-factorial-of-a-given-number-using-recursion

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
