CS 1063, Fall 2005
Factorial class

Here is an example of a more significant example that uses the BigInteger class. This is calculating the number 50! = 50*49*48*...*3*2*1.

// Factorial: calculate n!, for int n
package factorialpkg;
import java.math.*;

public class Factorial {
    public static void main(String[] args) {
        int n = 50;
        BigInteger bn = new BigInteger("1");
        for (int i = 2; i <= n; i++) {
            BigInteger mul = new BigInteger(i+"");
            bn = bn.multiply(mul);
        }
        System.out.println(n + "! = " + bn);
    }
}

Output:
50! = 30414093201713378043612608166064768844377641568960512000000000000