0

I have a simple program that is supposed to calculate the factorial of an entered number. It says I have no errors, but when I run the program it stalls and does not work. Can someone please tell me why?

    private void getFactorial_Click(object sender, EventArgs e)
    {
        int userNumber, numberFactorial = 1;

        userNumber = int.Parse(numberInput.Text);
        if (userNumber <= 0)
        {
            numberOutput.Text = numberFactorial.ToString();
        }
        else
        {
            for (int i = 1; 1 <= userNumber; i++)
            {
                numberFactorial = numberFactorial * i;
                numberOutput.Text = numberFactorial.ToString();
            }
        }
    }
}

}

Learnin2Code
  • 133
  • 1
  • 7
  • 14
  • What value are you providing for input? – dreamlax Oct 07 '13 at 00:16
  • it works now dreamlax. I had typed "1" where I needed to use "i" in my for loop. That was the issue. Thanks for the reply though – Learnin2Code Oct 07 '13 at 00:37
  • Side note: before you ask next one on this topic (like "why I get bad answer for 35") please check out http://stackoverflow.com/questions/13222207/why-computing-factorial-of-realtively-small-numbers-34-returns-0/13222515#13222515 – Alexei Levenkov Oct 07 '13 at 02:12
  • debug well before posting such a question.... – wam090 Oct 07 '13 at 08:49

2 Answers2

4

It's always the simple things that get us.

In your loop you have this:

for (int i = 1; 1 <= userNumber; i++)

I believe you mean this:

for (int i = 1; i <= userNumber; i++)

In the conditional part of the loop you used a '1' rather than 'i'.

Dweeberly
  • 4,668
  • 2
  • 22
  • 41
3

Your conditional is off for your for loop. I think you want i <= userNumber.

for (int i = 1; i <= userNumber; i++)
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445