Here you can find the source of Factorial(int x)
Parameter | Description |
---|---|
x | the number to have it's factorial calculated |
Parameter | Description |
---|---|
IllegalArgumentException | an exception |
public static int Factorial(int x) throws IllegalArgumentException
//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"); } } }