Mandy, One thing I'm not sure has been made clear is *why* you're getting an error.
In C++, numbers can be stored in a few different ways. It sounds to me like you're using an *int*, which can usually correctly store integers between about -2 billion and +2 billion. What happens when you try to store a number more than 2 billion? Your program doesn't fail, but it instead does something that might seem strange. Write a program like the following: int x = 2147483647; // The highest number an int can represent cout << x << endl; x = x + 1; cout << x << endl; Your program will probably output -2147483648. That's because in C++, your first number was stored, in binary, as a 32-bit number: 01111111111111111111111111111111 Then when you added 1 to it, you got: 10000000000000000000000000000000 ...which is how C++ represents -2147483648 using its two's complement<http://en.wikipedia.org/wiki/Two's_complement#Two.27s-complement_numbers>notation. If you were to keep adding one, you'd eventually get to: 11111111111111111111111111111111 ...which is how C++ represents -1. Add one to that, and you get: 00000000000000000000000000000000 ...which is how C++ represents 0. Notice that in binary, if you added 1 to that number, it would normally start with a 1: 100000000000000000000000000000000 ...but C++ only uses 32 bits to store an integer, so it drops the 33rd. So what you're seeing is an error that comes from the fact that C++ can't store a number bigger than about 2 billion in an int. Instead of an int you can use a *long long*, which can store larger numbers (up to about 9 billion billion, using 64 bits), but that will only get you up to 20 factorial. Look at some of the excellent suggestions in this thread for how to store numbers larger than that. As Luke pointed out, for example, the language Python will automatically use more bits when it needs to; or you could write a library in C++ that would let you use more bits. Best, Bartholomew P.S. If you look carefully, you'll probably see your program only calculated up to 13 factorial correctly. The number it computes for 14 factorial is actually *smaller*, because C++ couldn't store the larger number, and so it dropped all the bits after the 32nd bit. This is called "overflow". Once you get to 17!, you'll see that you got a negative number, because bit #32 happened to be 1. On Wed, Aug 3, 2011 at 1:19 AM, mandy <[email protected]> wrote: > Can anyone tell me how to compute factorial of 50 in c++ and even for > bigger no. > I am getting error(a default value) > > -- > You received this message because you are subscribed to the Google Groups > "google-codejam" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-code?hl=en. > > -- You received this message because you are subscribed to the Google Groups "google-codejam" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-code?hl=en.
