-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_FactorialsOfLargeNumbers.cpp
59 lines (51 loc) · 1.2 KB
/
GFG_FactorialsOfLargeNumbers.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
50
51
52
53
54
55
56
57
58
59
/*
https://practice.geeksforgeeks.org/problems/factorials-of-large-numbers2508/1#
Factorials of large numbers
*/
// { Driver Code Starts
// Initial Template for C++
#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> ans;
ans.push_back(1);
int val=0, carry=0;
for(int x=2; x<=N; x++)
{
carry = 0;
for(int i=0; i<ans.size(); i++)
{
val = ans[i]*x + carry;
carry = val/10;
ans[i] = val%10;
}
while(carry)
{
ans.push_back(carry%10);
carry /= 10;
}
}
reverse(ans.begin(), ans.end());
return ans;
}
};
// { 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