Factorial trail 0

Practice

Author: Aryan KD
Tester: Aryan KD
Editorialist: Aryan KD

DIFFICULTY:

CAKEWALK, SIMPLE, EASY.

PREREQUISITES:

Math .

PROBLEM:

Chef has given a number by his teacher and she told him to find a factorial of a number and then print the trailing Zeroe’s after finding a factorial of number for example given number is 5

factorial of 5 is

5 x 4 x 3 x 2 x 1 = 120

here number of trailing zeroe’s are 1 so the output is 1 .

EXPLANATION:

In above question one number N is given we have to find trailing zeroe’s
for that we have to find first factorial then count trailing zero on it but for N >20 it will show error according to constraints
but another way is that we have to just divide the number by 5 (while n>0) and add it to one variable sum and in the last print sum it will show the desired output.
for example n=5 is given
it is divide by 5 then n=1 remains
add n=1 in sum now sum becomes 1
further divide n by 5 now n becomes 0
add 0 to sum now sum becomes 1+0=1
now n =0 so the loop will turminated our desired output in sum print it
5!=120 and trailing zero is 1 and our output is 1 .

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
long long int n,s=0;
cin>>n;
while(n!=0){
n=n/5;
s+=n;
}
cout<<s;

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Tester's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
long long int n,s=0;
cin>>n;
while(n!=0){
n=n/5;
s+=n;
}
cout<<s;

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Editorialist's Solution

#include<bits/stdc++.h>
using namespace std;
void testcase()
{
long long int n,s=0;
cin>>n;
while(n!=0){
n=n/5;
s+=n;
}
cout<<s;

}
int main()
{
int t;
cin>>t;
while(t–)
{
testcase();
cout<<endl;
}
return 0;
}

Codechef (https://www.codechef.com/users/aryan0106)
aryan0106 | CodeChef User Profile for Aryan KD | CodeChef