Time limit of a c program while calculating factorial of numbers in c
The best solution is only to compute the factorials you need and only compute them once.


Read all the user input find the maximum value.
Generate the factorials for all values upto the max
Note: Saving them as you go:
Print the factorial values by looking up the result you generated in 2.


So lets look at the code:

I would not bother dynamically allocating the data storage.

    b=(int *)malloc(n*sizeof(int));


The question specifically limits the maximum value of n to 100.

    int b[101]; // should be suffecient


When reading values this should work (if you assume that the input is good).

        scanf("%d",&amp;b[i]);


But the question states that the input is one value per line. Personally I would validate that there is one value per line.

Here you are calculating the factorial multiple times:

    for(i=0;i&lt;n;i++)
       factorial(b[i]);  // Factorial is O(n)
                         // Thus this loop is O(n^2)


Technically you only need to call factorial once. If you calculate factorial for 'n' you need to calculate the factorial for 'n-1' (its how it is defined). If you store the numbers then you only need to look up the values to print it once they have been calculated.

It seems like you calculate this value each time.

    float p=0.0;
    for(i=2;i&lt;=N;i++)
    p=p+log10(i);


The worst case scenario is that you need to calculate this for a value of 100. You only need to calculate this one for 100. Also you can calculate it off line and now it is a const expression.

This is then a const expression and thus you don't need to calculating it.

    b=(int *)malloc(d*sizeof(int));


But you do need 100 of them so you can store all intermediate values:

You don't need to loop over an array to initialize it:

    for(i=1;i&lt;d;i++)
        b[i]=0;


You can initialize it already zero'd out. For dynamic memory use calloc() for automatic arrays use = {0};

So the above two can be combined into;

    const int d = &lt;pre-calculated value&gt;;
    int b[100][d] = {0};
    b[0][0] = 1;
    b[1][0] = 1;