Reputation: 61
I am new to C programming I want to find out the number of trailing zeros in a Factorial of given number
{
long int n=0,facto=1,ln=0;
int zcount=0,i=1;
printf("Enter a number:");
scanf("%ld",&n);
if(n==0)
{
facto=1;
}
else
{
for(i=1;i<=n;i++)
{
facto=facto*i;
}
}
printf("%ld",facto);
while(facto>0)
{
ln=facto%10;
facto/10;
if(ln=!0)
{
break;
}
else
{
zcount+=1;
}
}
printf("Tere are Total %d Trailing zeros in given factorial",zcount);
}
I have tried to calculate the modulo of the the number which will return the last digit of given number as the remainder and then n/10;
will remove the last number.
After executing the program the output always shows number of trailing zeros as "0",The condition if(ln =! 0)
always gets satisfied even if there is a zero.
Upvotes: 1
Views: 8761
Reputation: 31
I have solved this kind of problem, I think your question is just find the number of trailing zeros of a factorial number like - 15! = 1307674368000 if you look at the trailing 3 digits which are 000
Efficient code is
int n;cin>>n;
int ans = 0;
while(n)
{ ans += (n = n/5);}
cout<<ans;
Here one should look for int overflow, array bounds. Also, the special cases are n=1?
Upvotes: 0
Reputation: 2817
Try this
int trailingZeroCountInFactorial(int n)
{
int result = 0;
for (int i = 5; i <= n; i *= 5)
{
result += n / i;
}
return result;
}
To learn, how it works, there is a great explanation here
Upvotes: 0
Reputation: 1
package lb;
import java.util.Scanner;
public class l1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int a=n/5;
if(a<5)
{
System.out.println(a);
}
else
{
int c=a/5+a;
System.out.println(c);
}
}
}
Upvotes: 0
Reputation: 843
Let us remember math (wiki):
int trailingZeroCountInFactorial(int n)
{
int result = 0;
for (int i = 5; i <= n; i *= 5)
{
result += n / i;
if(i > INT_MAX / 5) // prevent integer overflow
break;
}
return result;
}
Upvotes: 5
Reputation: 300
{
long int n=0,facto=1,ln=0;
int zcount=0,i=1;
printf("Enter a number:");
scanf("%ld",&n);
if(n==0)
{
facto=1;
}
else
{
for(i=1;i<=n;i++)
{
facto=facto*i;
}
}
printf("%ld\n",facto);
while(facto>0)
{
ln=(facto%10);
facto/=10; //here you done one mistake
if(ln!=0) //another one here
{
break;
}
else
{
zcount+=1;
}
}
printf("Tere are Total %d Trailing zeros in given factorial",zcount);
} /Run this code it will work now and mistakes you already knows i guess/
Upvotes: -2
Reputation: 2619
The correct syntax in
if(ln!=0)
{
// do something
}
In if ( ln=!0 )
, first !0
is evaluated then =
assigns !0
(which is 1
) to ln
, then if
checks the value of ln
, if the value is non-zero(in this case it is 1
due to previous assignment) then break
statement is executed.
Also, this approach will not work for numbers larger than 25.
It is obvious that the power of 5 in n!
will be the answer.
The following expression gives the power of a prime p
in n!
:-
f(n/p) + f(n/p^2) + f(n/p^3) + ..... (until f(n/p^k) return zero
, where f(a)
returns the greatest integer of a
or the floor of a
Upvotes: 0
Reputation: 53026
This
if(ln=!0)
means ln = !0
i.e. you are assigning !0
to ln
so the condition is always true, change it to
if (ln != 0)
you can use an assignment as a truth value, but if you are sure you are doing it.
To prevent accidentally doing it, turn on compiler warnings.
Upvotes: 1