Fctrl: time limit exceeded error

#include
using namespace std;

int main()

{

int i;
cin>>i;
while(i--)
{
	int a,s=1;
	cin>>a;
	while(--a)
		s=s*(a+1);
	int b=0;
	while(s%10==0)
	{	
		b+=1;
		s/=10;
	}
	cout<<b<<endl;

}
    return 0;

}

Hi Naveen,

You don’t need to calculate the whole factorial in order to find the no. of trailing zeros. Just doing floor division with the input by 5 continuously till the input becomes 0, will fetch you the answer.
PS: For large inputs, use long.

#include <iostream>
using namespace std;
int main()
{
    int i;
    cin>>i;
    for(int j=0; j<i; j++)
    {
        long a;
        int zeros=0;
        cin >> a;
        while(a!=0)
        {   
            zeros=zeros+(a/5);
            a=a/5;
        }
        cout<<zeros<<endl;
    }
    return 0;
}

Regards, Aadarsh.

THANKS AADARSH!!

No problem Naveen!! Pls close this post by making it the accepted answer. :slight_smile:

Thanks, Aadarsh…