please help.....my code for small factorials is not working..why is it so ?

#include<stdio.h>
int main()
{
int n,t,i,j,a[200],k,temp=0,x,m=1;
scanf("%d",&t);
for(j=1;j<=t;i++)
{
scanf("%d",&n);
a[0]=1;
for(i=1;i<=n;i++)
{
for(k=0;k<m;k++)
{
x=a[k]*i+temp;
a[k]=x%10;
temp=x/10;
}
if(temp!=0)
{
while(temp!=0)
{
k=m;
a[k]=temp/10;
m++;
}
}

	}
	
}

for(j=1;j<=t;j++)
{
for(k=m;k>=0;k–)
{
printf("%d",a[k]);
}
printf("\n");
}
return 0;
}

It’s difficult to read your code next time mark the code part as a code.

here is my code

#include<iostream>
using namespace std;

int main(){
    int n, t;
    int a[160], x;

    cin>>t;
    while(t--){

        int m=1, z, temp=0;
        a[0]=1;

        cin>>n;
        for(int i = 1; i <= n; i++){

        for(int j = 0; j< m; j++){
            x= a[j]*i + temp;
            a[j]= x%10;
            temp = x/10;
        }
        while(temp!=0){
            a[m] = temp%10;
            temp = temp/10;
            m++;
            }
            }
        for(int q= m-1; q>=0; q--)
            cout<<a[q];
        cout<<"\n";
        }
}

Pretty much same logic as yours. If you face any problem, comment below.

PS If a answer helps you please don’t hesitate to up vote it.

1 Like

i guess there is something wrong in the while(temp!=0)loop bcoz ur not changing the value of temp at all!

#include<stdio.h>
int main()
{
int n,t,q,i,j,a[200],k,temp=0,x=0,m=1;
scanf("%d",&t);

for(j=1;j<=t;j++ )
{
	a[0]=1;
	scanf("%d",&n);
	
	for(i=1;i<=n;i++)
	{
		
		for(k=0;k<m;k++)
		{
			
			x=a[k]*i+temp;
			a[k]=x%10;
			temp=x/10;
		}
		while(temp!=0)
		{
			a[m]=temp%10;
			temp=temp/10;
			m++;
		}
	}
	for(q=m-1;q>=0;q--)
	{
		printf("%d",a[q]);
		
	}
}

/for(j=1;j<=t;j++)
{
for(k=m-1;k>=0;k–)
{
printf("%d",a[k]);
}
printf("\n");
}
/

}

1 Like

could you please tell me whats wrong in this code…?? i have changed my code or could you just write your code in c.

#include<stdio.h>
int main{ int n,t,q,i,j,k,temp=0,x=0,m=1; 
        scanf("%d",&t);
        for(j=1;j<=t;j++ )
        {   int a[200]={0};
                a[0]=1;
                scanf("%d",&n);
            for(i=1;i<=n;i++)
            {

                    for(k=0;k<m;k++)
                    {

                            x=a[k]*i+temp;
                            a[k]=x%10;
                            temp=x/10;
                    }
                    while(temp!=0)
                    {
                            a[m]=temp%10;
                            temp=temp/10;
                            m++;
                    }
            }
            for(q=m-1;q>=0;q--)
            {
                    printf("%d",a[q]);

            }
            printf("\n");
    }
    return 0;
}

This is your corrected code.I have made 2 corrections:

  1. Declaring int a[200] inside the for loop and initializing it to 0.
  2. printf("\n"); at the end

I think you can think of the two corrections made,if you have any doubts comment below!

#include<stdio.h>

int main()
{
int n,t,q,i,j,a[200],k;

scanf("%d",&t);

for(j=1;j<=t;j++ )
{
// you need to reset the value of temp, x and m each time

int temp=0,x=0,m=1;
a[0]=1;
scanf("%d",&n);

for(i=1;i<=n;i++)
{

    for(k=0;k<m;k++)
    {

        x=a[k]*i+temp;
        a[k]=x%10;
        temp=x/10;
    }
    while(temp!=0)
    {
        a[m]=temp%10;
        temp=temp/10;
        m++;
    }
}
for(q=m-1;q>=0;q--)
{
    printf("%d",a[q]);
}
// here a new line
    printf("\n");

}
}