A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Factorial in D page! Here, you'll find the source code for this program as well as a description of how the program works.
// Factorial program in D
import std.stdio: writeln;
import std.conv: to, ConvException;
import core.stdc.stdlib: exit;
void usage()
{
writeln("Usage: please input a non-negative integer");
exit(0);
}
int fac(int a)
{
if (a <= 1)
{
return 1;
}
return a*fac(a-1);
}
int main(string[] args)
{
if (args.length < 2)
{
usage();
}
int depth;
try {
depth = to!int(args[1]);
if (depth < 0)
{
usage();
}
}
catch(ConvException _)
{
usage();
}
int result = fac(depth);
writeln(result);
return 0;
}
Factorial in D was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.