I recently encountered a problem in the beginner question FCTRL2.
This is my code:
#include <stdio.h>
int main()
{
int t,i,j,n,fact=1,l;
scanf("%d",&t);
int a[t-1];
for(i=1;i<=t;i++)
{
scanf("%d",&n);
for(j=1;j<=n;j++)
{
fact=fact*j;
}
a[i-1]=fact;
fact=1;
}
for(l=0;l<=(t-1);l++)
{
printf("%d\n",a[l]);
}
return 0;
}
This produced the desired output when I ran it in Dev C++. However, when I submit my question, they keep telling it is a wrong answer.
I would like to know whether my approach to the problem is wrong or not.
Well it should give wrong answer indeed. You are using int data type which has a range of approximately 10^9 but the constraints of the problem are upto 100 .
100!=93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
So obviously this integer won’t fit in int data type and for that matter it won’t even fit in long long int or unsigned long long int .
So you need to change your approach and once you are done with coding check the value of 100! once to get an idea whether your code is correct or not.
Thanks for your feedback. I understood my problem. If it were Java or Python I could use the Big Integer class. I am not sure whether they work on C/C++. I guess the safest way were to treat my numbers as strings and carry on with my code, if I am correct.