Factorials

This program is doing the factorial of a number and for some reason whenever i type in 13 i get an error......The Factorial of you number, 13,is:
terminate called after throwing an instance of 'int'
Abort trap....It works for 12 and lower but nothing above that. can someone please explain to me why?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cstdlib>

using namespace std;

int factorial(int n)
{
        static int cur;
        if (n== 1)
        {
                return 1;
        }

        cur = number * factorial(n - 1);
        if (cur > 500000000)
        {
                throw number;
        }

        return cur;
}

int main(int argc, char** argv)
{
        int fact;
        cout << "Please Enter A Number:" << endl;
        cin >> fact;

        cout << "The Factorial of you number, " << fact << ",is:" << endl;
        try
        {
                factorial(fact);
        }
        catch (int over)
        {
                cerr << "ERROR: Overflow at n = " << over << "." << endl; exit(EXIT_FAILURE);
        }

        cout << factorial(fact) << endl;
}


Note:
13! = 6,227,020,800 so 13! > 500,000,000
12! = 479,001,600 so 12! <= 500,000,000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(int argc, char** argv)
{
        ...
        try
        {
                factorial(fact);
                cout << factorial(fact) << endl; //you invoke the method twice, hence you throw the int twice.
                  // you might want to store the result instead (see below)
        }
        catch (int over)
        {
                cerr << "ERROR: Overflow at n = " << over << "." << endl; exit(EXIT_FAILURE);
        }

        // cout << factorial(fact) << endl; //moved above
}
or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

int main(int argc, char** argv)
{
        ...
        int f = -1;
        try
        {
                f = factorial(fact);
        }
        catch (int over)
        {
                cerr << "ERROR: Overflow at n = " << over << "." << endl; exit(EXIT_FAILURE);
        }

        if( f > 0 )
          cout << f << endl;
}
i did both of the methods you gave me and it still has an overflow at 13....but if i change the 500,000,000 to 5,000,000,000 it gives me an error too
Well the precision of a signed int is only in [-2^31, 2^31 - 1] or from -2,147,483,648 to 2,147,483,647
So 5,000,000,000 is not a valid int
Notice the statement cerr << "ERROR: Overflow at n = " << over << "." << endl;
look at where it is, and think about when it will execute...
Like said Mathhead200, you can't have 13! on int. Or at least not on 32-bit int! You need a bigger number container! Try the 64-bit "unsigned long long" for a few more factorials. Otherwise you have to resort to some third-party tools like Multiple Precision Arithmetic Library (http://gmplib.org), that will compute everything for you as long as you'll still have resources on your computer.
Topic archived. No new replies allowed.