forked from vedant1771/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FactorialofLargeNumber.cpp
49 lines (45 loc) · 1.02 KB
/
FactorialofLargeNumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution {
public:
vector<int> factorial(int N)
{
vector<int> v;
v.push_back(1);
for(int i=2; i<=N; i++)
{
int carry = 0;
for(int j=0; j<v.size(); j++)
{
int mul = (v[j] * i) + carry;
v[j] = mul % 10;
carry = mul / 10;
}
while(carry)
{
v.push_back(carry % 10);
carry = carry / 10;
}
}
reverse(v.begin(), v.end());
return v;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int N;
cin >> N;
Solution ob;
vector<int> result = ob.factorial(N);
for (int i = 0; i < result.size(); ++i){
cout<< result[i];
}
cout << endl;
}
return 0;
} // } Driver Code Ends