Why this code not working

#include
using namespace std;

int main() { int n,a,arr[n] ;
cin>>n;
for(int i=0;i<n;i++)
{cin>>a;int fac=1;
for(int j=a;j>0;j–)
{
fac=fac*j;
}
arr[i]=fac;
}
for(int i=0;i<n;i++)
cout<<arr[i]<<“\n”;

// your code goes here
return 0;
}

—> code shows runtime error(sigsegv) when arr[n] and wrong answer when arr[100]or ar[1000]
problem-FCTRL2 Problem - CodeChef

when i run the code on the compiler below. it shows correct output for the respective input

u are declaring array of size n before even assigning any value to n

what size should i assign to the aaray then? please help me out ,i tried 100 and 1000 . it shows wrong answer

Numbers large as 100! (100 factorial) can not be stored in int.
INT_MAX=2147483647 (i.e. 9-10 digits) while 100! is a 158 digit number.

1 Like

declare array size after input n .
like this

cin >> n;
int arr[n];

tell me what to do ,which data type

tried ,still not working

#include
using namespace std;

int main() { int n,a;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{cin>>a;int fac=1;
for(int j=a;j>0;j–)
{
fac=fac*j;
}
cout<<fac<<"\n";
}

// your code goes here
return 0;
}

i tried doing this by not taking an array ,still it shows wrong answer

Where is the Link to problem??

Some languages like python and java have inbuilt support for big integer types. But C/C++ do not have that support.
in C/C++, you will have to store each digit individually in an array and use basic multiplication technique.
See this : Find the Factorial of a large number - GeeksforGeeks

yes u are right bro but whats the problem in that ??

See maximum number you can store in unsigned 32 bit integer is 2^32–1 and in unsigned 64 bit integer is 2^64–1 i.e. 9-10 digits and 18-19 digits whereas 100! is 158 digits long (i.e. the numbers you are trying to store can not be stored in the datatypes available) .So you will have to use an array to store each individual digit.

ya…i know that but whats your quetion?