-
Notifications
You must be signed in to change notification settings - Fork 82
/
factorial_20.cpp
49 lines (47 loc) · 1007 Bytes
/
factorial_20.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
//Program to calculate factorial of integers greater than 20
#include <iostream>
#include <vector>
using namespace std;
vector<int> arr;
void fact(int);
int main()
{
int n;
cout << "Enter Number: ";
cin >> n;
arr.clear();
arr.push_back(1);
fact(n);
for (int i = 0; i < arr.size(); i++)
{
cout << arr[arr.size() - i - 1];
}
cout << endl;
return 0;
}
void fact(int n)
{
if (n != 1)
{
int carry = 0, test;
for (int i = 0; i < arr.size(); i++)
{
test = ((n * arr[i]) + carry) % 10;
carry = ((n * arr[i]) + carry) / 10;
arr[i] = test;
}
if (carry != 0)
{
if (carry < 10)
{
arr.push_back(carry);
}
else
{
arr.push_back(carry % 10);
arr.push_back(carry / 10);
}
}
fact(n - 1);
}
}