+ 3

Static void Fact(int n) in C#'s methods lesson

I think that there may be an error in the definition of Fact. 0! Is defined to be 1. The code in the question used "if(num == 1){....}", if you ask for 0! infinite recursion occurs. If the test was "if(num <= 1){...}, The problem is solved, see code fragment below. namespace SoloLearn { class Program { static int Fact(int num) { if (num <= 1) { return 1; } return num * Fact(num - 1); } ......}

23rd Jun 2018, 6:27 PM
Rick Shiffman
Rick Shiffman - avatar
1 ответ
+ 2
Yes you're right regarding the fact that 0! = 1. It really depends on the assumption of the integer provided as if we really want to be pedantic, negative numbers shouldn't be allowed as well. 😉
23rd Jun 2018, 11:52 PM
Zephyr Koo
Zephyr Koo - avatar