Computes the factorial of a number. - Java java.lang

Java examples for java.lang:Math Number

Description

Computes the factorial of a number.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int number = 2;
        System.out.println(factorial(number));
    }/*from ww w  . j  a va2s  .c o  m*/

    /**
     * Computes the factorial of a number.
     *
     * @param number   An integer whose factorial to compute.
     * @return   The factorial of the integer.
     */
    public static int factorial(int number) {

        if (number == 0) {
            return 1;
        } else {
            return number * factorial(number - 1);
        }

    }
}

Related Tutorials