Java Factorial factorial(int n)

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

Description

Returns n!

License

Open Source License

Parameter

Parameter Description
n an int

Return

n!

Declaration

public static int factorial(int n) 

Method Source Code

//package com.java2s;

public class Main {
    /** Returns n!/*  www. jav  a 2  s  .c o  m*/
     * @param n an int
     * @return n!
     */
    public static int factorial(int n) {
        int returnValue;
        if (n < 0) {
            throw new IllegalArgumentException("Illegal value: " + n);
        } else if ((n == 0) || (n == 1)) {
            returnValue = 1;
        } else {
            returnValue = n * factorial(n - 1);
        }
        return returnValue;
    }

    /** Returns n!
     * @param n a long
     * @return n!
     */
    public static double factorial(double n) {
        double returnValue;
        if (n < 0.0) {
            throw new IllegalArgumentException("Illegal value: " + n);
        } else if ((n == 0) || (n == 1)) {
            returnValue = 1.0;
        } else {
            returnValue = n * factorial(n - 1.0);
        }
        return returnValue;
    }
}

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 number)
  9. factorial(int value)