Factorial in Java

Published on 28 April 2022 (Updated: 25 September 2022)

Factorial in Java

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

Current Solution

public class Factorial
{
    public static long fact(long n)
    {
        if (n <= 0)
            return 1;
        return n * fact(n - 1);
    }

    public static void main(String[] args)
    {
        try
        {
            long n = Long.parseLong(args[0]);
            if (n > 59)
            {
                System.out.println(String.format("%1$s! is out of the reasonable bounds for calculation.", n));
                System.exit(1);
            }
            else if (n < 0)
            {
                System.out.println("Usage: please input a non-negative integer");
                System.exit(1);
            }
            long result = fact(n);
            System.out.println(result);
        }
        catch (Exception e)
        {
            System.out.println("Usage: please input a non-negative integer");
            System.exit(1);
        }
    }
}

Factorial in Java was written by:

If you see anything you'd like to change or update, please consider contributing.

Note: The solution shown above is the current solution in the Sample Programs repository as of Oct 16 2019 15:02:04. The solution was first committed on Dec 30 2018 17:30:18. As a result, documentation below may be outdated.

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.