public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int noftimes=sc.nextInt();
for (int i = 0; i < noftimes; i++) {
int value=sc.nextInt();
int count=getValue(value/5);
System.out.println(String.valueOf(count));
}
#include<stdio.h>
int fact(int);
int zeroes(int);
main()
{
int p,i,j,a,b;
printf(“enter the number of numbers to be entered”);scanf("%d",&p);
for(i=0;i<p;i++){scanf("%d",&j);a=fact(j);b=zeroes(a);printf("%d",b);}}
int fact(int j)
{int f;if(n==1)f=1;else f=f*fact(j-1);return f;}
int zeroes(int a);
{int y=0,z;while(z==0){z=a%10;if(z==0){y=y+1;a=a%10}}return y;}
#include<stdio.h>
int fact(int);
int zeroes(int);
main()
{
int p,i,j,a,b;
printf(“enter the number of numbers to be entered”);scanf("%d",&p);
for(i=0;i<p;i++){scanf("%d",&j);a=fact(j);b=zeroes(a);printf("%d",b);}}
int fact(int j)
{int f;if(n==1)f=1;else f=f*fact(j-1);return f;}
int zeroes(int a);
{int y=0,z;while(z==0){z=a%10;if(z==0){y=y+1;a=a%10}}return y;}
#include #include
using namespace std;
int main()
{
unsigned long long int t, n, i, maxpo, sum;
cin>>t;
while(t–)
{
cin>>n;
maxpo = sum = 0;
maxpo = (int)(log(n)/log(5));
for(i = 1;i <= maxpo;i++)
sum += n/pow(5,i);
cout<<sum<<endl;
}
}
instead of finding the factors why can’t we actually count the no of zero’s of that number using “number modulo 10” which gives us the last digit in that number
#include<stdio.h>
int calc (int entr);
int main(void)
{
long num;
scanf("%ld",&num);
long entr[num];
long fact[num];
for(int i=0;i<num;i++)
{
scanf("%ld",&entr[i]);
fact[i]=calc(entr[i]);
#include<stdio.h>
int calc (int entr);
int main(void)
{
long num;
scanf("%ld",&num);
long entr[num];
long fact[num];
for(int i=0;i<num;i++)
{
scanf("%ld",&entr[i]);
fact[i]=calc(entr[i]);
#include<stdio.h>
#include<math.h>
long int zeroes(long int a);
int main()
{
int n,i=0;
long int k[100000];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%ld\n",&k[i]);
}
for(i=0;i<n;i++)
{
zeroes(k[i]);
}
return 0;
}
long int zeroes(long int a)
{
int z=1,b=0,i=0;
while(a>5*z)
{
z=pow(5,i+1);
b+=a/z;
i+=1;
}
printf("%ld\n",b);
}
Could someone please tell me what is wrong with this code.
Basic idea behind my logic is i calculate the factorial by multiplying all numbers from 1 to N(N being the number itself) and at each juncture i check the condition whether the factorial at that particular instant is divisible by 10 leaving a remainder of 0 and if it is i am calculalating the sum at the end.
here goes the code.
int main()
{
int a,b,c,fact=1,sum=0,e=0;
cin>>a;//for taking in the total number of input
int f=0;
for(b=0;b<a;b++)
{
cin>>c;//selecting each input value
@dark_pharoah Your logic is correct but it takes more time, quite cumbersome and is prone to mistakes. The logic is 0’s are formed by combination of 5’s and 2’s(since 5x2=10) Agree? For example 30=2x3x5(0 is formed by 5 and 2). So, we have to find min(number of 2’s,number of 5’s) in n! so that they can combine to form 0’s. That can be found by:-
number of 2’s->[n/2]+[n/2^2]+… until it is 0
number of 5’s->[n/5]+[n/5^2]+… until it is 0, Here [] is floor
The reason for this is explained clearly in the above editorial. Have a look at it.
We can observe that number of 5’s will be less as it’s a big number(so just calculate number of 5’s)
int mmofive( long int t)
{ int n=0, pof=5;
while ( t/pof>0)
{pof*=5; n++;}
return n-1;
}//max divisible multiple of 5 (power)
int main()
{
long int n,t; int i,j;
int trailingzero=0;
cin>>n;
const long int f=n;
int A[f];
for ( i=0; i<n; i++)
{cin>>t;
for(j=5;j<=5^mmofive(t);j*=5)
{trailingzero+=t/j;}
A[i]=trailingzero;
trialingzero=0;
}
for (long int i=0; i<n; i++)
{cout<<A[i]<<endl;}
return 0;
}
WHY NOT WORKING? :((
i devised the same logic as expected.
but it says it exceeds time limit.
i believe, even if the number is as large as 100000000 , on dividing by 5 continuously, the final output should take quite less time.
Help in C#!!
My Code is Running successfully, giving desires o/p
But on submit getting error as Wrong Answer.
using System;
public class Test
{
public static void Main()
{// your code goes here
int n = Convert.ToInt32(Console.ReadLine());
int[] N = new int[n];
for(int i=0;i<N.Length;i++)
{
N[i] = Convert.ToInt32(Console.ReadLine());
}
for(int i=0;i<N.Length;i++)
{
int fact = 5;
int trailingzeros = 0;
while(N[i]>fact)
{
trailingzeros = trailingzeros + (N[i]/fact);
fact = fact*5;
}
Console.WriteLine(trailingzeros);
}
}
}
#include <iostream>
using namespace std;
int main()
{
// your code goes here
int f=9;
do
{
int n;
cin>>n;
int fac=1;
for(int i=1; i<=n; i++)
{
fac= fac*i;
}
int cont=0;
while(fac%10==0)
{
cont++;
fac= fac/10;
}
cout<<cont<<endl;
} while(f==9);
return 0;
}
Why can’t I simply find the answer by cout<<(input/5)+(input/25); please explain it to me somebody? Why do I need to divide it if I can find the answer by doing the above method. But if I am trying to do (input/5)+(input/25) I am getting WA. Please help!