FCTRL Error

Here is my code for the problem where we need to find the number of trailing zeros in the factorial of a given number. Link here

#include<bits/stdc++.h>
using namespace std;
long long z(long long a)
{//number of trailing zeroes in a!
	int sum=0,t,b=1;
	while(t!=0)
	{
		t=a/pow(5,b);
		b++;
		sum=sum+t;

	}
	return sum;
}
int main() {
	long long t,a;

	cin>>t;

	while(t--)
	{
		cin>>a;
		cout<<z(a)<<endl;
	}
	return 0;
}

I am not getting a correct answer. Is there something wrong in the logic or am I missing something minute?

You are not initializing t . t can be 0 initially and so it will not go in the loop .
Here is your corrected code .

#include<bits/stdc++.h>
using namespace std;
long long z(long long a)
{//number of trailing zeroes in a!
    int sum=0,t=1,b=1;
    while(t!=0)
    {
        t=a/pow(5,b);
        b++;
        sum=sum+t;

    }
    return sum;
}
int main() {
    long long t,a;

    cin>>t;

    while(t--)
    {
        cin>>a;
        cout<<z(a)<<endl;
    }
    return 0;
}
1 Like
//