Gets the factorial of a number - Java java.lang

Java examples for java.lang:Math Number

Description

Gets the factorial of a number

Demo Code


//package com.java2s;

public class Main {
    /**//from  w  w w  . j  a v a 2 s  .  co m
     * Gets the factorial of a number
     *
     * @param num Number to start at
     * @return The factorial
     */
    public static long factorial(long num) {
        if (num == 0)
            return 1;

        if (num == 2 || num == 1) {
            return num;
        } else {
            return num * factorial(num - 1);
        }
    }
}

Related Tutorials