Java Factorial factorial(int n)

Here you can find the source of factorial(int n)

Description

Find the factorial of the input number

License

Open Source License

Parameter

Parameter Description
n a positive number to find the factorial of

Exception

Parameter Description
IllegalArgumentExcpetion for negative inputs

Return

the factorial of the number

Declaration

public static int factorial(int n) 

Method Source Code

//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;
        }
    }
}

Related

  1. factorial(int n)
  2. factorial(int n)
  3. factorial(int n)
  4. factorial(int n)
  5. factorial(int n)
  6. factorial(int n)
  7. factorial(int n)
  8. factorial(int n)
  9. factorial(int n)