Factorial in D

Published on 04 October 2020 (Updated: 13 May 2023)

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.

Current Solution

// 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.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.