Saturday, 25 July 2015

FCTRL

Standard

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.

RECOMMENDED QUESTION :

Try this question afer completing this.

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;

    }

2 comments:

  1. Can you help me with solving this problem recursively?

    ReplyDelete
    Replies
    1. Here is a recursive solution :

      #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;

      }

      Delete