-
Notifications
You must be signed in to change notification settings - Fork 508
/
Copy pathFactorials of large numbers.cpp
68 lines (58 loc) · 1.05 KB
/
Factorials of large numbers.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
60
61
62
63
64
65
66
67
68
/*
Factorials of large numbers
===========================
Given an integer, the task is to find factorial of the number.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N,the number whose factorial is to be found
Output:
Print the factorial of the number in separate line.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
Example:
Input:
3
5
10
2
Output:
120
3628800
2
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
string ans = "1";
for (int i = 2; i <= n; ++i)
{
int carry = 0;
for (auto &ch : ans)
{
int num = ch - '0';
num = i * num;
num += carry;
int digit = num % 10;
ch = (digit + '0');
carry = num / 10;
}
while (carry)
{
int digit = carry % 10;
carry = carry / 10;
ans += to_string(digit);
}
}
reverse(ans.begin(), ans.end());
cout << ans << endl;
}
return 0;
}