-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2_bigfactorial.cpp
More file actions
59 lines (43 loc) · 1.09 KB
/
2_bigfactorial.cpp
File metadata and controls
59 lines (43 loc) · 1.09 KB
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
/*
Factorials for even small numbers like 50 or 100 or 200 consists of
around 65, 158, 375 digits respectively.
Storing such large numbers is not possible in c++
An Array is used to store each digit of the number and then
the elements of the array are printed which long story short, results
in the printing of the element
Order used for N! is 1*2*3*.....*ns
E.g. : 5 !
Initialize the array's first element as 1
After each iteration
1st iteration : {1};
2nd iteration : {2};
3rd iteration : {6};
4th iteration : {4, 2} which is actually 24 when printed backwards
5th iteration : {0, 2, 1} which is 120
To find Factorials of very big numbers, just increase the array size
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n; cin >> n;
int arr[200], m = 1;
arr[0] = 1;
for(int i=1; i<=n; i++)
{
int temp = 0;
for(int j=0; j<m; j++){
int x = arr[j]*i + temp;
temp = x / 10;
arr[j] = x % 10;
}
while(temp != 0){
arr[m] = temp % 10;
temp /= 10;
m++;
}
}
for(int i=m-1; i>=0; i--)
cout << arr[i];
cout << endl;
}