- Course
- Recursion
- Calculate the factorial of a number
Calculate the factorial of a number
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that implements a recursive function that receives a requested number from the user and returns the factorial of that number. You must calculate the factorial of the number using recursion.
Input
6
Output
720
Solution
using System;
public class CalculateFactorial
{
public static void Main(string[] args)
{
int num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(Factorial(num));
}
public static int Factorial(int num)
{
if (num == 0)
{
return 1;
}
else
{
return num * Factorial(num - 1);
}
}
}