Calculates factorial. - Java java.lang

Java examples for java.lang:Math Algorithm

Description

Calculates factorial.

Demo Code

// Copyright (C) GridGain Systems, Inc. Licensed under GPLv3, http://www.gnu.org/licenses/gpl.html
//package com.java2s;
import java.math.*;

public class Main {
    /**//  w  ww  . j  a va2 s.  c  o m
     * Calculates factorial.
     *
     * @param num Number for factorial.
     * @return Factorial.
     */
    static BigInteger factorial(int num) {
        BigInteger fact = new BigInteger("1");

        for (int i = 2; i <= num; i++)
            fact = fact.multiply(new BigInteger(String.valueOf(i)));

        return fact;
    }
}

Related Tutorials