Factorial
Link to the question : FCTRL
HINT :
For finding the number of trailing zeroes, we need to find how many factors of 10 are present. Since 10 = 2 *5 , we basically need to find the number of factors of 5, since its obvious that factors of 2 will be greater in number.
SOURCE CODE :
#include<stdio.h>
#include<math.h>
int main()
{
int t,c,i;
long n;
scanf("%d",&t);
while(t--)
{
c=0;i=1;
scanf("%ld",&n);
while(n/pow(5,i)>=1)
{
c=c+(int)(n/pow(5,i));
i=i+1;
}
printf("%d\n",c);
}
return 0;
}
Can you help me with solving this problem recursively?
ReplyDeleteHere is a recursive solution :
Delete#include
#include
int f(long n, long power)
{
if(power>n)
return 0;
int ans;
ans = n/power;
return ans + f(n, power*5);
}
int main()
{
int t,c,i;
long n;
scanf("%d",&t);
while(t--)
{
c=0;i=1;
scanf("%ld",&n);
printf("%d\n",f(n,5));
}
return 0;
}
This comment has been removed by the author.
ReplyDelete