fctrl 2: can anyone help with me with following code? i think its correct but error being given as wrong answer

  • #include<stdio.h>
    int factorial[80]; int length;

        void fact(int n) { 	int i,j,sum,temp;
    	for(i=1;i<80;i++) 		factorial[i]=0;
    	factorial[0]=1; 	length=1;
    	for(i=2;i<=n;i++) 	{ 		j=temp=0;
    		while(j<length) 		{
    			sum=temp+factorial[j]*i;
    			factorial[j]=sum%100; 			j++;
    			temp=sum/100; 		} 		while(temp>0)
    		{ 			factorial[j++]=temp%100;
    			temp/=100; 			length++; 		} 	} }
        void print(void) { 	short int i;
    	printf("%d",factorial[length-1]);
    	for(i=length-2;i>=0;i--) 	{
    		if(factorial[i]>=10)
    			printf("%d",factorial[i]); 		else
    			printf("0%d",factorial[i]); 	}
    	printf("\n"); }
        int main() { 	short int i,n,t;
    	scanf("%d",&t); 	for(i=1;i<=t;i++)
    	{ 		scanf("%d",&n); 		fact(n);
    		print(); 	} 	return 0; }

In the main() function change the “short int” to “int”.That should work.Your program is not exiting the for loop present in main() because of that short int.

1 Like