title: Time limit of a c program while calculating factorial of numbers in c
body: <p>I am solving a problem on calculation of factorial and the challenge is as follows!</p>
  
  <pre><code>You are asked to calculate factorials of some small positive integers.
  
  Input
  
  An integer t, 1&lt;=t&lt;=100, denoting the number of testcases, followed by t lines,
  each containing a single integer n, 1&lt;=n&lt;=100.
  
  Output 
  
  For each integer n given at input, display a line with the value of n!
  </code></pre>
  
  <p>My Code is giving me the correct solution but Time limit is exceeded which is 2 seconds:</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;math.h&gt;
  void factorial(int N)
  {
      printf("\n\n");
      int q,i,j,t,d,z;
      float p=0.0;
      for(i=2;i&lt;=N;i++)
      p=p+log10(i);
      d=(int)p+1;//No of terms in the factorial
      int *b;
      //initialization of an array
      b=(int *)malloc(d*sizeof(int));
      b[0]=1;
      for(i=1;i&lt;N;i++)
      b[i]=0;
      //calculation of factorial
      p=0.0;
      for(j=2;j&lt;=N;j++)
      {
          q=0;
          p=p+log10(j);
          z=(int)p+1;
          for(i=0;i&lt;N;i++)
          {
             t=(b[i]*j)+q;
             q=t/10;
             b[i]=t%10;   
          }
      }
      for(i=d-1;i&gt;=0;i--)
      printf("%d",b[i]);
  
  }
  int main()
  {
      int n,i,j;
      scanf("%d",&amp;n);
      int *b;
      b=(int *)malloc(n*sizeof(int));
      for(i=0;i&lt;n;i++)
      {
          scanf("%d",&amp;b[i]);
      }
      for(i=0;i&lt;n;i++)
      factorial(b[i]);
      return 0;
  }
  </code></pre>
  
  <p>How can i make my program more efficient and produce the output in the given time limit?
  This challenge is from <a href="http://www.hackerearth.com/problem/algorithm/small-factorials/" rel="nofollow">HackerEarth</a></p>