Java Factorial Factorial(int x)

Here you can find the source of Factorial(int x)

Description

returns the factorial of a number

License

LGPL

Parameter

Parameter Description
x the number to have it's factorial calculated

Exception

Parameter Description
IllegalArgumentException an exception

Return

x factorial

Declaration

public static int Factorial(int x) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**/* www.  ja v  a  2 s  . c  o  m*/
     * returns the factorial of a number
     * 
     * @param x
     *            the number to have it's factorial calculated
     * @return x factorial
     * @throws IllegalArgumentException
     */
    public static int Factorial(int x) throws IllegalArgumentException {
        if (x == 0 || x == 1) {
            return 1;
        } else if (x > 0) {
            int mul = 2;
            for (int i = 3; i <= x; i++) {
                mul *= i;
            }
            return mul;
        } else {
            throw new IllegalArgumentException("cannot calculate the factorial of a negative number");
        }

    }
}

Related

  1. factorial(int n)
  2. factorial(int n)
  3. factorial(int number)
  4. factorial(int value)
  5. factorial(int x)
  6. factorial(int x)
  7. factorial(long l)
  8. factorial(long n)
  9. factorial(long num)