fork download
  1. #include <iostream>
  2.  
  3. template <int N>
  4. struct Factorial
  5. {
  6. enum
  7. {
  8. value = N * Factorial<N - 1>::value
  9. };
  10. };
  11.  
  12. template <>
  13. struct Factorial<0>
  14. {
  15. enum
  16. {
  17. value = 1
  18. };
  19. };
  20.  
  21. unsigned int factorial(unsigned int n)
  22. {
  23. return (n == 0)? 1 : n * factorial(n - 1);
  24. }
  25.  
  26. using namespace std;
  27.  
  28. int main()
  29. {
  30. const int x = factorial(0);
  31. const int y = factorial(10);
  32. const int z = Factorial<0>::value;
  33. const int v = Factorial<10>::value;
  34.  
  35. cout << x << endl
  36. << y << endl
  37. << z << endl
  38. << v << endl;
  39.  
  40. return 0;
  41. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
1
3628800
1
3628800