Header Ad

HackerEarth Small Factorials problem solution

In this HackerEarth Small Factorials problem solution, You are asked to calculate factorials of some small positive integers.


HackerEarth Small Factorials problem solution


HackerEarth Small Factorials problem solution.

#include<bits/stdc++.h>



#define ll long long
#define ull unsigned long long
#define repA(i,a,n) for(int i=a;i<n;i++)
#define repD(i,a,n) for(int i=n-1;i>=a;i--)

#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)



using namespace std;
#define MAX 500
int res[MAX];

int multiply(int x, int res[], int res_size);

void factorial(int n)

{

res[0] = 1;

int res_size = 1;
for (int x=2; x<=n; x++)

{

res_size = multiply(x, res, res_size);

}



for (int i=res_size-1; i>=0; i--)

cout << res[i];

}



int multiply(int x, int res[], int res_size)

{

int carry = 0;



for (int i=0; i<res_size; i++)

{

int prod = res[i] * x + carry;

res[i] = prod % 10;

carry = prod/10;

}



while (carry)

{

res[res_size] = carry%10;

carry = carry/10;

res_size++;

}

return res_size;

}



int main(){

fast;

int t;
cin>>t;
while(t--){



int n;
cin>>n;
factorial(n);
cout<<"\n";

}

}



Post a Comment

0 Comments