fork download
  1. import std.stdio;
  2.  
  3. void main()
  4. {
  5. for(int i=0; i<8; i++)
  6. writefln("%s! = %s", i, factorial(i));
  7. }
  8.  
  9. template factorial(T)
  10. {
  11. T factorial(T n)
  12. {
  13. if (n <= 1)
  14. return cast(T)1;
  15. else
  16. return cast(T)( n * factorial(n-1));
  17. }
  18. }
Success #stdin #stdout 0.01s 2120KB
stdin
Standard input is empty
stdout
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040