fctrl2:codechef says" giving a wrong answer" while the program is perfectly fine..please help





#include <stdio.h>
#include <stdlib.h>

int main()

{

int i,j,t,n,f;

scanf("%d",&t);

for(i=0;i<t;i++)

{

	scanf("%d",&n);

	 f=1;

	for(j=1;j<=n;j++)
	{
    	f=f*j;

	}
        printf("%d\n",f);
}

return 0;

}

Plz see the constraints carefully

can u please let me know the changes in the program?

hey @avrupa

About constraints:

Input
An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100.

So that mean your code should be able to calculate maximum of 100 factorial and factorial of 100 is

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L

which cannot be stored in integer or floating point datatypes in c or c++, So you have to use array to store the number.

Now ho to do that ?

For this problem check this link: discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-a-tutorial

Hope you understood…! :slight_smile:

Well! The int data type can store a no. of 10^9 magnitude
And do you think 100! Can fit inside that :slight_smile:
Try thinking different :slight_smile:

you cant store 100 factorial in any primitive data type in C and C++ .

thanx! that helped.

Simplest way is to think about how you can create a program to multiply two numbers i.e. 22 x 44 = 968
and save the value in a character array.
22
x44 = multiply right ‘2’ of 22 with 44,calculate the 88%10 feed in array and in small classes there was a term called ‘carry’ while solving multiplication and that carry here is 88/10. (initially carry = 0 ). Then multiply left '2 with 44, add carry, calculate %10 and rest of the things as above
For more help refer to the editorials or comment here.

most welcome… :slight_smile: