chore(CSharp): added algorithm to determine factorial (#437)

pull/447/head
Waqar Hassan Khan 2021-09-03 20:42:30 +06:00 committed by GitHub
parent 2835574020
commit 7ce3167f87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 0 deletions

View File

@ -16,3 +16,6 @@ For running the `.cs` file please using [.Net Finddle](https://dotnetfiddle.net/
## Maths ## Maths
- [Abundant Number](src/Maths/abundant-number.cs) - [Abundant Number](src/Maths/abundant-number.cs)
## Recusrsion
- [Factorial](src/Recursion/factorial.cs)

View File

@ -0,0 +1,27 @@
using System;
namespace Algorithms.Recursion
{
public class Factorial
{
public static int FactorialUsingRecursion(int number)
{
if(number == 0 || number == 1)
{
return 1;
}
return number * FactorialUsingRecursion(number - 1);
}
public static void Main()
{
Console.WriteLine($"Factorial of 7 is: {FactorialUsingRecursion(17)}");
}
}
}
/*
* n! = n * (n - 1) * (n - 2) * .... * 3 * 2 * 1
* we have used int here but for larger number, int will not be enough
*/

View File

@ -0,0 +1,18 @@
using NUnit.Framework;
namespace Algorithms.Tests.Recursion
{
[TestFixture]
public class FactorialTest
{
[TestCase(0, 1)]
[TestCase(7, 5040)]
[TestCase(3, 6)]
[TestCase(1, 1)]
public void TestFactorial_ShouldGetExpectedResult(int number, int expected)
{
int result = Algorithms.Recursion.Factorial.FactorialUsingRecursion(number);
Assert.AreEqual(expected, result);
}
}
}