JAVA语言: 设计算阶乘的函数为:m(n)=n!
来源:百度知道 编辑:UC知道 时间:2024/06/03 15:37:24
设计算阶乘的函数为:
m(n)=n!
试设计一个计算阶乘的方法和相应的类。并利用这个类计算m(2),m(14),m(28)。
要求用JAVA语言编写
m(n)=n!
试设计一个计算阶乘的方法和相应的类。并利用这个类计算m(2),m(14),m(28)。
要求用JAVA语言编写
import java.math.BigInteger;
public class Factorial
{
public static BigInteger calc(int n)
{
BigInteger ret = BigInteger.ONE;
for (Integer i= 2; i<=n; i++) {
ret = ret.multiply(new BigInteger(i.toString()));
}
return ret;
}
public static void main(String[] args) {
System.out.println("Factorial of 2 is: " + Factorial.calc(2) );
System.out.println("Factorial of 14 is: " + Factorial.calc(14) );