Not Yet Answered # number factorial and their multiplication

HG&C Mitja Bonca 557 ddanbe 2,503 Discussion Starter raizor89

0

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**************

*Edited 5 Years Ago by raizor89*: missed a condition

0

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

*Edited 5 Years Ago by HG&C*: n/a

0

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";
```

*Edited 5 Years Ago by Mitja Bonca*: n/a

0

```
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);
}
}
```

0

```
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 !!

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...