FCTRL2 PROBLEM

c-language

#1

/* WHERE IS THE PROBLEM IN THIS CODE FOR FCTRL2 PROBLEM
IT’S WORKING FOR CUSTOM INPUT BUT SHOWING IT WRONG
CAN ANY ONE HAVE SOME IDEA TO SHARE */
#include <stdio.h>

int main(void) {
int i;
scanf("%d",&i);
while(i–)
{
int j,k;
scanf("%d",&j);
int fact=1;
for(k=1;k<=j;k++)
{
fact=fact*k;
}
printf("%d \n",fact);
}

// your code goes here
return 0;

}


#2

For problems like these, which appear very simple at first glance, you need to think twice about what the problem is really asking for.

In this case, you need to note that 13! itself is a really big number, roughly around 6*10^{9}, so the INT datatype cannot store it. It will overflow. You should keep a practice of testing your code with sample inputs, other than what have been provided as Sample Test Cases. Even if you use LONG LONG INT, it only has a range of 9*10^{18}, which is still not enough for large factorials. Factorials increase very rapidly.

To avoid these kind of problems, you should usually just check if the maximum input, i.e. in this case, n=100 gives the correct result. But 100! is around 9*10^{157}, so you can’t really store it in any datatype, but what you can do, and what you need to do for this problem is, store digits of the required number separately. Since 100! is around 9*10^{157} it means you can use 160 digits to sufficiently represent all your answers. Now you need to write algorithms to multiply a number with your digit-wise stored huge number.
You can see this solution I wrote and learn about using Arrays/Vectors to store big numbers.


#3

you can do code in python there is no limit of range but in c language integer value can contain only 10^6 value not more than that


#4

use python and go for recursion :innocent: