阶乘

2018-07-14  本文已影响0人  Ridiculous_one

问题

输入一个正整数 n,输出 n! 的值。
其中 n! = 1 x 2 x 3 x … x n,n! < Integer.MAX_VALUE

解答

public class Factorial
{
    public static void main (String[] args)
    {
        Factorial f = new Factorial();
        System.out.println( f.calculate(12) );
    }
    
    public int calculate (int n)
    {
        int result = 1;
        for (int i = 1; i < n+1; i++)
        {
            result = result * i;
        }
        return result;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读