HELP A BEGINNER PLEASE :| !! simple factorial

Please help. All I had to do was find factorial of n numbers and print but i Still keep getting wrong answer i.e Red Cross as output!!! I’m a starter so please dont judge :smiley:

Problem code beginner FCTRL2


using namespace std;

int main()
{  int t;
   int n,x,r;
   scanf("%d", &t);

   for(int i=0;i<t;i++)
       { r=1;




     for(int j=0;j<t;j++)

return 0;

There is a memory limit on how much a variable can store. In can store ROUGHLY a little more than 10^9 while long long int can store roughly around 10^18 . (9 x 10^18, You can google it for exact number.)

The numbers after 20! are too large to be stored. Your logic is correct but in implementation, you face a overflow error. You need to know trick of “Handling large numbers in c++”

However, if you submit same code in python, same logic and all, you should get AC.

I believe you have not give enough thought to this problem. It asks you to calculate the factorial of numbers in range [1,100].

Here are few points you need to consider to suggest why you get wrong answer :

  1. Try writing down first 20 factorials. You should come to the conclusion that factorials grow rapidly fast. How large then you imagine 100! would be? In fact 100! has over 150 decimal digits.
  2. You should read about memory storage in C/C++. Every datatype supports a specified range and not beyond that. It’s basically a overflow error. Some links : link link
  3. Now that you know that you can’t store such a large answer ( even with long long, maximum stored result can be of 18~19 digits), you need to approach it in a different way (think about strings!)

If you are still unable to solve, refer to this editorial : editorial

The question asks you to find the factorial of a number N, where 1 <= N <= 100.

The function f(x) = x! increases rapidly as x increases.

alt text

For example :-


Now, refer to the following link for the ranges of the datatypes in C++.

As you can see, the range of int is much less to store a number as big as 100!.

Now, the problem is how to store such big numbers?

  1. You can store each digit as an element in an array. There is already a nicely written post about it by @kuruma []

  2. You can use a different language that supports BigIntegers, like Java, Python, etc.

1 Like

Don’t use int. Use a long long and even unsigned long long if need be.
I think that this would be enough for the mentioned problem but if someday you need 64 bit integers, use int64_t.

Use of these data types will not solve the problem if used in the above naive code as they still cannot store those big numbers.

1 Like

long long already are 64 bit integers. There is no advantage for using int64_t.