# What is the fault in this code?

//Code for calculating the factorial of numbers
#include

using namespace std;

int main()
{
//Enter the number of test cases
int t;
cin >> t;
int arr[t]{0};
for(int i=0;i<t;i++)
{
//Enter the value of numbers.
int n;
cin >> n;
int j=n;
int s=1;
//Computing the value of their factorials
while(j!=0)
{
s=s*j;
j–;
}
arr[i]=s;
}
cout <<"--------------------------------------"<<endl;
//Printing the result.
for(int j=0;j<t;j++)
{
cout << arr[j] <<endl;
}
return 0;
}
It shows correct answer using custom input but shows “Wrong Ans” While submitting the solution.

int arr[t]={0} //wrong declaration of array
j–;

Nah…
Coz its accepting and giving the correct answer using custom input.

You may not have taken into consideration the constraints of the problem. The final answer may not fit into an “int” variable and may result into buffer overflow. n! is an expression whose size grows at a very large rate. Even 20! will not fit into an int variable.

I used long long int array to store the factorial of numbers and long long int s to store each numbers factorial,still it shows wrong answer during submission but accepts and gives the correct answer using custom input

You are expected to calculate factorials of numbers that are as large as 100.
Let’s consider the worst case. Did you know how large 100! is? That’s right, 158 digits!
Do you think even `unsigned long long int` can hold that? I don’t think so.
So, let’s give you a tiny hint. How about considering a string of digits representing each number, accompanied by multiplication?

2 Likes

hey format the code [Tutorial] CoLoR format the Code!

That’s really smart Can you help me in my code?

https://discuss.codechef.com/t/are-there-any-limitations-to-the-use-of-array-ed-version-of-classes-in-c/