Not Yet Answered # factorial fuctions

chococrack 74 Discussion Starter kuchick32 -3 Narue 5,707 Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

0

5 factorial:

5! = 5 x 4 x 3 x 2 x 1 = 120

4! = 4 x 3 x 2 x 1 = 24

15! = 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 1307674368000

To get the other values ( for 0 and then for negative ), first check if the number is greater than 0, if it is, then do the factorial. If it isn't check if it is equal to zero, if so, " The factorial of 0 is 1 " otherwise "Invalid Input"

Good enough?

0

5 factorial:

```
5! = 5 x 4 x 3 x 2 x 1 = 120
4! = 4 x 3 x 2 x 1 = 24
15! = 15 x 14 x 13 x 12 x 11 x 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 1307674368000
```

To get the other values ( for 0 and then for negative ), first check if the number is greater than 0, if it is, then do the factorial. If it isn't check if it is equal to zero, if so, " The factorial of 0 is 1 " otherwise "Invalid Input"

Good enough?

so far I have

```
int factorial (int n);
//Returns factorial of n.
//The argument n should be nonnegative
{
int product = 5;
for (n > 0)
{
product = n!;
n--;
}
return product;
}
```

written but i'm not sure where to go from here. it is still giving me an error

*Edited 3 Years Ago by mike_2000_17*: Fixed formatting

0

There's no operator for a factorial in C++, you need to perform the multiplications manually. You have the right idea though, now it's just a matter of details. Here are a couple of things to consider down the road:

- This factorial function accepts a signed integer argument, which means n could be a negative value. A robust implementation would handle negative values sensibly.
- Factorials are a great way to overflow your data type because they grow so quickly. An overflow check would be a fantastic idea in the case of an n that's too large for int.

**>int product = 5;**

Start the product at n rather than any hard value. Also, be sure to check for 0 as 0! is 1.

**>for (n > 0)**

Anything multiplied by 1 is itself, so you can stop the loop before n reaches 1.

**>product = n!;**

Obviously this should be `product = product * n;`

. Otherwise a factorial function would be unnecessary. ;)

**>n--;**

product is set to n originally, so you need to decrement n *before* the multiplication step. As it is you'll start off with `n * n`

rather than `n * n - 1`

.

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

Recommended Articles

Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.

btn_addline :

`Private Sub btn_addline_Click(ByVal ...`

I am writing a java program that needs to execute shell commands, so I wrote a function that would take the command to execute as a string (ie: "mkdir ~/Folder1") and execute that command with the shell. Here is the function:

```
try
{
Runtime run = Runtime.getRuntime();
Process pr = ...
```