Discussion About Trailing zeroes

This is Discussion thread about Trailing zeroes

can you explain the question and check my code

public class tracingZero {
public static void main (String args[]) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int facnum=fac(n);
int count=0;
while(facnum!=0)
{
int rem=facnum%10;
if(rem==0)
{
count++;
}
facnum=facnum/10;
}
System.out.println(count);

    }
	public static int fac(int n)
	{
       if(n==1)
	   {
		   return 1;
	   }
	   int num=n*fac(n-1);
	   return num;
	}

}

Hello guys can you check the problem in my code .
int main () {
int count = 0;
int n;
cin>>n;
if(n >= 5){
for(int i = 5; i <= n; i += 5){
count += log(i)/log(5);
}
}
cout<<count;
return 0;
}

// this code runs fine still there is error show
#include <bits/stdc++.h>
#include
using namespace std;
int main(){
int n,number=1,remainder,countt=0,reversee=0,q=0;
cin>>n;
int a=1;
while(n>0){
a=a*n;
n=n-1;
}
//cout<<a<<endl;

while(a>0){
	remainder=a%10;
	reversee=reversee*10+remainder;
	a=a/10;
	if(remainder==0){
		countt=countt+1;
	}q=countt;
}cout<<q;

}

this is not the best approach as u see we are trying to find a factorial of a number of order 10^9 and max we can store in long long int is of order 10^18 so if we try to compute n factorial it exceeds the limit that’s why our code is incorrect. this question is not about doing all the computation it has a simple logic any number has zeroes in it if it is a multiple of 10 right.
so we have a formula which says
no of tailing zeros is (num/5 + num/5^2 +num/5^3 …and so on this stops when num/5^i ==0)
try this :smiley:

this is not the best approach as u see we are trying to find a factorial of a number of order 10^9 and max we can store in long long int is of order 10^18 so if we try to compute n factorial it exceeds the limit that’s why our code is incorrect. this question is not about doing all the computation it has a simple logic any number has zeroes in it if it is a multiple of 10 right.
so we have a formula which says
no of tailing zeros is (num/5 + num/5^2 +num/5^3 …and so on this stops when num/5^i ==0)
try this :smiley:

1 Like

why is this code not working?
#include

using namespace std;

int main() {

int n,i,k;

cin>>n;

k=0;

for(i=1;i<=n;i=i++){

    if (i%5==0){

        while (i!=5){

        k=k+1;

        i=i/5;}

    }

}

return 0;

}

Can anyone tell me the error in my code. 2 test cases failed out of 4. Here is my Java code:

import java.util.*;
import java.math.BigInteger;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
BigInteger fact = factorial(n);
BigInteger count = new BigInteger(“0”);
BigInteger digit = fact.mod(BigInteger.valueOf(10));
while(digit.intValue()==0){
count = count.add(BigInteger.valueOf(1));
fact = fact.divide(BigInteger.valueOf(10));
digit = fact.mod(BigInteger.valueOf(10));
}
System.out.println(count);
}

static BigInteger factorial(int n) {
	BigInteger x = new BigInteger("1");
	for(int i=2;i<=n;i++) {
		x = x.multiply(BigInteger.valueOf(i));
	}
	return x;
}

}

What theorem/concept is this based upon?