Small factorial (in practice - easy)

#include<stdio.h>
#define max 150
int arr[max],length=1;
factorial(int);
main()
{
int T=0 ,n=0,i=0,j=0,k=0;
int temp,x;
int out[150];
scanf("%d",&T);

arr[0] = 1;
for(i=0;i<T;i++)
{
    scanf("%d",&n);
    factorial(n);

    for(j=length-1;j>=0;j--)
       {
             out[k] = arr[j];
             k++;
       }

        out[k] = 100;
        k++;
        arr[0]=1;

       for(j=length-1;j>0;j--)
       {

           arr[j]=0;
       }
       length=1;




}


    for(j=0;j<k;j++)
       {
           if(out[j]==100)
           {
               printf("\n");
           }
           else
            printf("%d",out[j]);
       }

}
factorial(int n)
{
int i,j,x,temp=0;
for(i=2;i<=n;i++)
{
for(j=0;j<length;j++)
{
x= arr[j]* i + temp;
arr[j] = x%10;
temp = x/10;

    }
        if(temp !=0)
        {
              while(temp!=0)
               {
                 length++;
                 arr[j] = temp%10;
                 temp = temp /10 ;
                 j++;
               }

        }


}

}


my code works fine for even 400! but im getting and error of “Runtime Error(SIGSEGV)” while submmitting the answer can anybody plz help me out here and tell me what to be corrected ? …

check this

I guess, 100! will need more than 150 digits… Try increasing the array size…

I don’t think so your code would be able to provide an output of 400! or even 100!.

See this :

alt text

Just observe this pic length of 100! is of 158 decimal numbers,

and as you are saying about 400!, its length is 869…

So there will definitely be a segmentation fault [SIGSEGV] as you have defined the array of MAXSIZE 150.

Just try uploading your code again by changing MAXSIZE to 200.

Best of Luck…!! :slight_smile:

Thanx for the idea freinds i just changed the size of “out” array to 7489(nearly rough idea) by for worst case scenario when test cases would be 100 and factorial is calculated. “arr” array size to 160 as @rishabhprsd7 & @ajjinkya1p3 pointed out. and it worked fine.

If you think you got the answer then you may select the answer you like and close the thread…:slight_smile:

And welcome…!! :slight_smile:

Discussion forum is there to help everyone…:slight_smile: