Please help me with this code for Small Factorial

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

int main()
{
        int *num,j;
        long long  *fact;
        int t,i;
        num=(int *)malloc(t*sizeof(int));
        fact=(long long *)malloc(t*sizeof(long long));
        scanf("%d",&t);
        for(i=1;i<=t;i++)
        {
                  scanf("%d",&num[i]);
                  fact[i]=1;
        }
        for(i=1;i<=t;i++)
        {
                  for(j=num[i];j>=1;j--)
                  {
                            fact[i]=j*fact[i];
                  }
        }
        for(i=1;i<=t;i++)
        {
                         printf("\n%lld",fact[i]);
        }
        system("PAUSE");
        return 0;
}               

What is wrong with this code??? Cant understand…

Your code works for only those values of factorials which can fit in long long range.
For eg. 100! has around 160 digits which easily exceeds the long long range.

You are required to think like an elementary school kid :slight_smile:

1 Like

This problem has a tutorial http://www.codechef.com/wiki/tutorial-small-factorials Kindly refer to it.

1 Like

Hey, in malloc() you are using ‘t’ before inputting it. Isn’t it wrong…?

Hey go through the tutorial for Small Factorial provided at the beginning of the problem. It will help you. It sure did to me.