Input
An integer t, 1<=t<=100, denoting the number of test cases, followed by t lines, each containing a single integer n, 1<=n<=100.
Output
For each integer n given at input, display a line with the value of n!
I have typed the code below and it doesn't get accepted on codechef.
I don't know why.
#include<stdio.h>
double fact(int n);
void disp(double t);
int main() {
int a, i, c[100];
scanf("%d", &a);
for (i = 0; i < a; i++) {
scanf("%d", &c[i]);
}
for (i = 0; i < a; i++) {
disp(fact(c[i]));
}
return 0;
}
double fact(int n) {
double f;
if (n == 1 || n == 0)
f = (double) 1;
else
f = (double) n * fact(n - 1);
return f;
}
void disp(double t) {
printf("%f\n", t);
}
I have typed the code below and it doesn't get accepted on codechef.
I don't know why.
OP's code fails as double
lacks precision for this task. Given "containing a single integer n, 1<=n<=100." and fact(100)
, the printed result needs far more precision than afforded by double
.
100! is exactly
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
double
(precision) and uint64_t
(range) are both insufficient for this task. Need new approach. Something that extends exact integer multiplication.
Perhaps create a function that multiplies a string? mult_str(char *s, int n)
? "1"
* 2 --> "2"
.
Do-able in about 30 lines of C code without special libraries.