-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathlarge_factorials.c
52 lines (40 loc) · 1.07 KB
/
large_factorials.c
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
#include <stdio.h>
// This function multiplies x with the number represented by digit[].
// len is size of digit[] or number of digits in the number represented by digit[]
// simple elementary school multiplication
long long int multiply(long long int x, long long int digit[], long long int len)
{
long long int carry = 0, i, prod;
for (i = 0; i < len; i++)
{
prod = digit[i] * x + carry;
digit[i] = prod % 10;
carry = prod / 10;
}
while (carry){
digit[len] = carry % 10;
carry = carry / 10;
len++;
}
return len;
}
int main()
{
long long int t, i, n;
// t= no. of test cases, n= no. whose factorial is to be found out
scanf("%lld", &t);
while (t--)
{
scanf("%lld", &n);
long long int j = 1, digit[20000] = {0}, len = 1;
digit[0] = 1;
for (j = 1; j <= n; j++)
{
len = multiply(j, digit, len);
}
for (i = len - 1; i >= 0; i--)
printf("%lld", digit[i]);
printf("\n");
}
return 0;
}