forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.cs
More file actions
26 lines (24 loc) · 716 Bytes
/
Factorial.cs
File metadata and controls
26 lines (24 loc) · 716 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System;
namespace Algorithms.Numeric
{
/// <summary>
/// The factorial of a positive integer n, denoted by n!,
/// is the product of all positive integers less than or equal to n.
/// </summary>
public static class Factorial
{
/// <summary>
/// Calculates factorial of a number.
/// </summary>
/// <param name="num">Input number.</param>
/// <returns>Factorial of input number.</returns>
public static long Calculate(int num)
{
if (num < 0)
{
throw new ArgumentException("Only for num >= 0");
}
return num == 0 ? 1 : num * Calculate(num - 1);
}
}
}