about n!

One of my exercise is to calculate a number's factorial;
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;
int main()
{
int a=1,i,n;
cin>>n;
for(i=1;i<=n;i++)
{a=a*i;
}
cout<<a;
return 0;
}

when i try to calculate a number a litter bigger,the code returns 0.why?
Could you be a bit more specific? What numbers were working, and which weren't? The code itself looks fine, except that the user is never told what they need to input and if you aren't running this from the command line, the window will likely close before you can see the output.
closed account (z05DSL3A)
you are using an int to hold your n! but it has a upper limit of +2,147,483,647. Larger values of n will soon make this overflow.

12! is about the highest you will get with an int.
Just being curious I ran a test and the largest number I could get to factorial was 32!
That was using a double datatype.
http://www.cplusplus.com/forum/general/7630/

This was recently discussed.
Topic archived. No new replies allowed.