fork(1) download
  1. #include <stdio.h>
  2.  
  3. long long factorial (int n) {
  4. long long f[22] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000, 20922789888000, 355687428096000, 6402373705728000, 121645100408832000, 2432902008176640000, 51090942171709440000};
  5. return f[n];
  6. }
  7.  
  8. int main(void) {
  9. for (int i = 0; i <= 21; i++) printf("Factoral of %i is %lld\n",i , factorial(i));
  10. printf("21! overflows a long long, so you only need a lookup table up to 20!");
  11. return 0;
  12. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Factoral of 0 is 1
Factoral of 1 is 1
Factoral of 2 is 2
Factoral of 3 is 6
Factoral of 4 is 24
Factoral of 5 is 120
Factoral of 6 is 720
Factoral of 7 is 5040
Factoral of 8 is 40320
Factoral of 9 is 362880
Factoral of 10 is 3628800
Factoral of 11 is 39916800
Factoral of 12 is 479001600
Factoral of 13 is 6227020800
Factoral of 14 is 87178291200
Factoral of 15 is 1307674368000
Factoral of 16 is 20922789888000
Factoral of 17 is 355687428096000
Factoral of 18 is 6402373705728000
Factoral of 19 is 121645100408832000
Factoral of 20 is 2432902008176640000
Factoral of 21 is -4249290049419214848
21! overflows a long long, so you only need a lookup table up to 20!