Small factorials||codechef||beginner solution ||programming info

Small factorials

You are asked to calculate factorials of some small positive integers.

Input

An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100.

Output

For each integer n given at input, display a line with the value of n!

Example

Sample input:
4
1
2
5
3
Sample output:
1
2
120
6
Solution using C++ language:
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp> 
using namespace boost::multiprecision; 
using namespace std;


int main() 
{
 int t;
 cin>>t;
 
 while(t--)
 {
     int n;
     cin>>n;
     cpp_int f=1;
     for(int i=1;i<=n;i++)
     {
            f=f*i;
     }         
     cout<<f<<endl;
 }
 return 0;
}
alternate solution:-
#include <iostream>
using namespace std;

int main() 
    {
     int t;
     cin>>t;
     while(t--)
         {
             int n;
             cin>>n;
             int size=1000,fact[size],extra=0,j=size-1;
             fact[size-1]=1;
                      while(n>1)
                          {
                         int x;
                             for(int k=size-1;k>=j;k--)
                                 {
                                     x=fact[k]*n+extra;
                                     fact[k]=x%10;
                                     extra=x/10;
                                 }
                             while(extra>0)
                                 {
                                     fact[--j]=extra%10;
                                     extra/=10;
                                 }
                             n--;
                         }
                     for(int k=j;k<size;k++)
                         {
                             cout<<fact[k];
                         }
                     cout<<endl;
            }
     return 0;
    }

Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support