Hey everyone,

I have a task that I have to make a windows form application, that calculates the number factorial of any positive number, and the result shows the multiplication of factorials. Like here,
5! = 1*2*3*4*5* = 120

the form design is simple,
1. label1 ( enter the number)
2. textbox1
3. button1 ( named calculate // that shows the message in new messagebox.
the message also has to show if the input is wrong or right.
**************the condition is to use for or while loop method**************

Recommended Answers

All 6 Replies

the code in your button1 onclick METHOD (double click on button1 in design mode) will look something like this:

int numberToFactoral = 1;

try
{
    numberToFactoral = int32.parse(textbox1.text);
}
catch
{
     label1.text = "please input positive whole number";
     return;

}

int result = 1; // 1 * x is x so it wont change the result

//counter = the number to factor; while counter is bigger than 1 do this loop; counter -1;
for(int counter = numberToFactoral; counter >= 1; counter--)
{
     result *= counter; 
}

label1.text = result.toString();

here's what will happen if 3 is put into textbox1:

numberToFactoral = 3;
result = 1;
counter = 3;

3 is greater than or equal to 1 so
result (1) = result(1) * counter(3) so result is now 3
counter now equals 2

2 is greater than or equal to 1 so
result (3) = result(3) * counter(2) so result is now 6
counter now equals 1

1 is greater than or equal to 1 so
result (6) = result(6) * counter(1) so result is still 6
counter now equals 0

counter(0) is NOT greater than or equal to 1

label1.text shows 6

if you know the basics (as you should or are in the process of learning) you will easily be able to see what is going on and adjust to your program. I didn't test this code.
I hope this helps

This is partly true (decimals will not go through, while negative integers will go):

try
{
    numberToFactoral = int32.parse(textbox1.text);
}
catch
{
     label1.text = "please input positive whole number";
     return;

}

If the user will enter "-1" will go through try block. Because its -1 is still a "full number". but this will do:

int value = int.Parse(textBox1.Text);
            if(value<0)
                label1.text = "please input positive whole number";
private void button1_Click(object sender, EventArgs e)
        {
            string S = string.Empty;
            if (this.textBox1.Text != "")//minimal error detection
            {
                int n = int.Parse(this.textBox1.Text);
                int f = 1;
                S = this.textBox1.Text + "! = ";
                for (int i = 1; i <= n; i++) 
                { 
                    f *= i;
                    S += i.ToString() + "*";
                }
                // throw away last * and do final edit
                S = S.TrimEnd('*') + " = " + f.ToString();
                MessageBox.Show(S);
            }
        }
int N = 1;
            int S = 1;
            int X;
            X = int.Parse(textBox1.Text);
            if (X < 0)
            {
                MessageBox.Show("Invalid input for the function, please put positive number! ");
            }
            else
            {
                for (N = 1; N <= X; N++)
                {
                    S = S * N;


                }
                MessageBox.Show(X + "! = " + S);
            }

i have finally got the easy solution, thanks for you ideas fols.. :) cheers !!

What result do you get when you enter 16 into your application?

Even 14 is way too much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.