Factorial in C#

Published on 28 December 2018 (Updated: 16 October 2019)

Welcome to the Factorial in C# page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

using System;
using System.Numerics;

namespace SamplePrograms
{
    public class Factorial
    {
        public static BigInteger Fact(BigInteger n)
        {
            if (n <= 0)
                return 1;
            return n * Fact(n - 1);
        }

        public static void Main(string[] args)
        {
            try
            {
                var n = BigInteger.Parse(args[0]);
                if (n > 4550)
                {
                    Console.WriteLine(string.Format("{0}! is out of the reasonable bounds for calculation.", n));
                    Environment.Exit(1);
                }
                else if (n < 0) {
                    Console.WriteLine("Usage: please input a non-negative integer");
                    Environment.Exit(1);
                }
                var result = Fact(n);
                Console.WriteLine(result);
            }
            catch
            {
                Console.WriteLine("Usage: please input a non-negative integer");
                Environment.Exit(1);
            }
        }
    }
}

Factorial in C# 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.