# n!

Printable View

• 11-19-2002
sonict
n!
can someone get me started on calculating n! I think i should use a for loop.
:confused:
• 11-19-2002
the Wookie
where is the variable n used?

you need an equation

like
5 = 2 + n

or
2(n) + 12938109283109283 = 3094809384503985
• 11-19-2002
Sang-drax
Hehe I think the Wooike misunderstood the question! :D

n! = 1 * 2 * 3 * ... * n

If you know how to write for-loops this should be a piece of cake.
• 11-19-2002
MrWizard
By definition n factorial is...

n! = n * (n - 1) * (n - 2) * ... 1

The one at the end comes from the base case of 0! which is equal to 1. Have fun.
• 11-19-2002
alpha
i think you mean n factorial. probably one of the easiest ways is to use a recursive function...try it, then post your code...

it shouldn't be too long...
• 11-19-2002
sonict
n!
#include <iostream.h>
#include <stdio.h>

unsigned int f,x=0;

unsigned int factorial(unsigned int a);

int main(void)
{
int hold;
puts("Please enter an integer value: ");
scanf("%d", &x);

{2
f=factorial(x);
printf("%u factorial equals %u\n\n", x,f);
}
cin >> hold;
return 0;
}

unsigned int factorial(unsigned int a)
{
if(a==1)
{
return 1;
}

else
{
a *=factorial(a-1);
}

return a;
}
• 11-19-2002
Captain Penguin
Quote:

Originally posted by alpha
i think you mean n factorial. probably one of the easiest ways is to use a recursive function...try it, then post your code...

it shouldn't be too long...

You don't need a recursive function for that..
• 11-19-2002
the Wookie
Quote:

Originally posted by Sang-drax
Hehe I think the Wooike misunderstood the question! :D

n! = 1 * 2 * 3 * ... * n

If you know how to write for-loops this should be a piece of cake.

oooooooooo woops,

i thought he meant like

"help me!"

:o
• 11-19-2002
alpha
Quote:

Originally posted by Captain Penguin
You don't need a recursive function for that..
I know you don't need a recursive function for it, I think its a cooler way of doing it though...and possibly shorter, im not sure because i haven't written factorial using a for loop since a few months ago...
• 11-19-2002
Sang-drax
Quote:

Originally posted by MrWizard
By definition n factorial is...

n! = n * (n - 1) * (n - 2) * ... 1

The one at the end comes from the base case of 0! which is equal to 1. Have fun.

For fun, here's the definition of z! when z is a real or complex number:

When z is a negative integer, z! = infinity
• 11-19-2002
moi
Quote:

Originally posted by alpha
I know you don't need a recursive function for it, I think its a cooler way of doing it though...and possibly shorter, im not sure because i haven't written factorial using a for loop since a few months ago...
cooler? who cares, its sloooooooooooow.
• 11-19-2002
XSquared
Quote:

Originally posted by Sang-drax
For fun, here's the definition of z! when z is a real or complex number:

When z is a negative integer, z! = infinity

ewwwwww...

can you convert that to c code so i can understand it? :D
• 11-19-2002
Captain Penguin
Quote:

Originally posted by moi
cooler? who cares, its sloooooooooooow.
Yep, this is more than sufficient:

Code:

```int main() {   int n;   int ans = 1;   cin >> n;   for(int x = n; x > 0; x--)   {     ans = ans * x;   }   cout << ans; }```
• 11-19-2002
alpha
okay, i didn't know it was that much slower. i recently just kindof taught myself recursion, so I like using it for the time being...I'll get over the phase, hehe...