Created
September 11, 2023 04:05
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <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