Here you can find the source of factorial(int n)
Parameter | Description |
---|---|
n | a positive number to find the factorial of |
Parameter | Description |
---|---|
IllegalArgumentExcpetion | for negative inputs |
public static int factorial(int n)
//package com.java2s; //License from project: Open Source License public class Main { /**//from ww w. jav a 2 s . c o m * Find the factorial of the input number * @param n a positive number to find the factorial of * @return the factorial of the number * @throws IllegalArgumentExcpetion for negative inputs */ public static int factorial(int n) { if (n < 0) { throw new IllegalArgumentException("Input should be nonnegative."); } else if (n < 2) { return 1; } else { int accum = 1; for (int i = n; i > 0; i--) accum *= i; return accum; } } }