Java Factorial factorial(int n)

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

Description

factorial

License

Open Source License

Return

the n!

Declaration

public static long factorial(int n) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 University of Illinois at Urbana-Champaign and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*www . j a  v a  2  s .  c om*/
 *    UIUC - Initial API and implementation
 *******************************************************************************/

public class Main {
    /** @return the <i>n!</i> */
    public static long factorial(int n) {
        if (n < 0)
            throw new IllegalArgumentException();
        if (n < 2)
            return 1;

        long result = 1;
        for (int i = n; i >= 2; i--)
            result *= i;
        return result;
    }
}

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)