the following code appears to run OK on my machine (for the unconscionably few test points I tried) ....
It is not entirely clear to me what your intent was in "Main", and it's too late at night for me to check the code ... the Tonight Show is almost on. I note what appears at first glance to be a "console.ReadLine" in the middle of a WHILE loop, that must be fun, and an IF that seems questionable to this admittedly novice programmer ...Code:// **************************************** private long myVersionOfHisMain() { return factorial(Int32.Parse(textBox2.Text)); } private long factorial(long d) { long c = 0; long e = 1; long j = 1; while (c < d) { j = j * e; c++; e++; } return j; } // ******************************************************** and called as follows ... textBox1.Text = myVersionOfHisMain().ToString();
"if (n < 100) ;"
But your "factorial" function came up running (at least insofar as my HP calculator was able to confirm).
I had two versions of factorial ...
Code:// *************************************** private long calculate(int n) { if (n >= 0) if (n < 2) return 1; else return n * calculate(n - 1); else return -1; } // ************************************** // ************************************** private long AltCalculate(int n) { int cnt = 2; long product = 1; if (n >= 0) while (cnt <= n) { product *= cnt++; } else product = -1; return product; } // *************************************** called as follows ... textBox1.Text = calculate(Int32.Parse(textBox2.Text)).ToString();




Reply With Quote