Skip to content

Instantly share code, notes, and snippets.

@admir-live
Created September 11, 2023 04:05
Show Gist options
  • Select an option

  • Save admir-live/de70dd3502f4bcf867ac240529368711 to your computer and use it in GitHub Desktop.

Select an option

Save admir-live/de70dd3502f4bcf867ac240529368711 to your computer and use it in GitHub Desktop.
Calculates the factorial of a given non-negative integer using a recursive approach.
/// <summary>
/// Calculates the factorial of a given non-negative integer using a recursive approach.
/// </summary>
/// <param name="number">The non-negative integer for which the factorial is to be calculated.</param>
/// <returns>The factorial of the given number.</returns>
/// <exception cref="ArgumentException">Thrown when the provided number is negative.</exception>
public static int CalculateFactorial(int number)
{
if (number < 0)
{
throw new ArgumentException("Number must be a non-negative integer.");
}
if (number == 0 || number == 1)
{
return 1;
}
return number * CalculateFactorial(number - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment