factorial trailing c++

#include
using namespace std;
int fact(int );
int main()
{
int n,i,t,q;
cin>>n;
for(i=0;i<n;i++)
{
cin>>t;
int count =0;
q=fact(t);
while(q%10==0)
{
q=q/10;
count++;
}
cout<<count;
}
return 0;
}
int fact(int a)
{
int i=1,j;
for(j=1;j<=a;j++)
i=i*j;

return i;

}

can you be more specific what do you want to ask .

You are suffering from overflow because numbers after 20! cannot be stored in any data type in C++. You need to make some observations on the question, like when does a trailing 0 come in the factorial etc.

1 Like

There’s no need to find the factorials. Just count the number of 5’s in n!, that would be equal to the number of 0’s in n!.
(This is because each 2 & 5 pair form one 0, and number of 5’s is than that of 2’s.)