Sure, here is the C# code to create a function to calculate the factorial of a given number using recursion:
C#
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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is the C# code to create a function to calculate the factorial of a given number using recursion:
C#
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.