Calculate the factorial of n Pre: 0 <= n <= 12 - Java java.lang

Java examples for java.lang:Math Algorithm

Description

Calculate the factorial of n Pre: 0 <= n <= 12

Demo Code


//package com.java2s;

public class Main {
    /**//w w  w.j  a  v  a2 s  . c  o  m
     * Calculate the factorial of n
     * Pre: 0 <= n <= 12
     * @param n The integer whose factorial is being computed
     * @return n!
     */
    public static int factorial(int n) {
        int result = 1;
        while (n > 0) {
            result *= n--;
        }
        return result;
    }
}

Related Tutorials