Search code examples
c#factorial

c# SIMPLE factorial program


private void button1_Click(object sender, EventArgs e)
{
        String UserNumber = this.textBox1.Text;
        int NewUserNumber = Convert.ToInt32(UserNumber);
        int result = 0;
        int second = 0;

        while (NewUserNumber >= 1) 
        {

            result = NewUserNumber * (NewUserNumber - 1);
            NewUserNumber--;
        }
        String i = Convert.ToString(result);
        this.textBox2.Text = i;
    }
}

While I understand this is homework for me, I am stuck. I really really don't want this solved, I want to do it myself.

I don't understand why it's not working.. It's outputting 2 no matter what I put in.

I could do this in Java easily but the converting gets to me..

Any help would be great.


Solution

  • Your problem is not in the conversion. Please look at

    result = NewUserNumber * (NewUserNumber - 1);