Fatorial function

Hello, I'm doing a fatorial function like this:


unsigned long long fatorial(int a)
{
xi = 1;
rod = 0;
for (f=0; f<a; f++)
{
xi *= rod + 1;
rod++;
}
return (xi);
}

At the beggining fot like fatorial(5) it's ok, the function gives me 120, and for other numbers, but for like fatorial(17) it gives a crazy number that isn't the 17 fatorial. And if I do it without 'unsigned', it gives me a NEGATIVE number. WTF??

I even tried this:


cout << 1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17;


and it gives the number: -288522240

:(
xi shall be defined as unsigned long long.
a little googling here and there should do the trick, if u really know how would u look for it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

double fatorial(int a)
{
double xi = 1;
int rod = 0;
for (int f=0; f<a; f++)
{
xi *= rod + 1;
rod++;
}
return (xi);
}

int main ()
{
    cout.precision(0);
    cout << fixed << fatorial(17);
    cin.get();
    return 0;
}
@OP: overflow
@dumb0t: imprecise

By the way, `rod' and `f' have the same values; there is no need for both.
Last edited on
Topic archived. No new replies allowed.