I'm trying to pass a test case as below:
using System;
using System.Collections.Generic;
using NUnit.Framework;
[TestFixture]
public class SolutionTests
{
[Test]
public void Test1()
{
var solution = new Solution();
Assert.AreEqual(solution.Factorial(5), 120);
}
}
My code is returning 3125 and the expected answer is 120.
My code is below and I'm not sure why it's not working.
using System;
using System.Collections.Generic;
using System.IO;
public class Solution
{
public int Factorial(int input)
{
int result = 1;
for (int i = 1; i <= input; i++)
{
result = result * input;
}
return result;
}
}
I have looked at other similar examples but I'm struggling to understand them due to my learning difficulties can someone please help
You should multiply the result on i
and not on input
in the for
loop like this:
for (int i = 1; i <= input; i++)
{
result = result * i;
}