factorial....please help. my code is working for small values of n but not for large values

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

for(j=0;j<t;j++ )
{
	scanf("%d",&n);
	a[0]=1;
	m=1;
	temp=0;
	c=0;
	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=0;q<m;q++)
	{
		if(a[q]==0)
		c++;
		else
		break;
	}
	printf("%d",c);
	printf("\n");
}

return 0;

}

Here is my version, you can easily convert it into c. just change cin>> by scanf() and cout<< by prinf

#include<iostream>
using namespace std;

int z(int a){
	int l=0,n;
	n=5;
	while(n<=a){
	l = l + a/n;
	n = n*5;
	}
	cout<<l<<"\n";
}

int main(){
	int t,a;
	cin>>t;
	while(t--){
		cin>>a;
		z(a);
	}
	return 0;
}

The logic is for checking no of trailing zeroes-
For zero in a no there must be a 5,10,15,20… . Here, you will see all are the multiples of 5.

Algo

- count the multiples of n=5

- If no is grater than n = 5^2 then count the multiples of 25

- continue with n=5^3, 5^4 …

- break when n > a (the no)

For ex a = 60

n=5

k = 60/5 = 12

now n = 55 = 25*

k = k + 60/25 = 14

answer = 14

If you need any help comment below and next time post the question link also.
Upvote and select as a answer if it helped.

2 Likes

ok i got it…but…could you please tell me whats wrong in my code?

@harry_potter -->are you trying to do the counting zero’s in the factorial,problem ?

@msvas07 -->yes because we are supposed to count zeros in this problem

@harry_potter maybe after using long long you will get TLE. use the one that i provided

error in your code
scanf("%d",&n);

size of N -> 1 <= N <= 1000000000.

You need to use long long instead of int

does it helped?

Actually there is a very simple solution for that. what you are doing is a very complex method.