Java求阶乘和
作者:互联网
public static int sumFactorial(int n) {
//求1!+2!+3!+4!+........+n!的和
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += factorial(i);
}
return sum;
}
public static int factorial(int n) {
//求n!
if (n == 1) {
return 1;
}
return n*factorial(n-1);
}
public static void main(String[] args) {
System.out.println(sumFactorial(3));
}
输出结果:
标签:Java,int,sum,factorial,static,阶乘,return,public 来源: https://blog.csdn.net/weixin_46429649/article/details/120873314