/*计算阶乘:N!+(N-1)!+(N-2)!.....1*/
public class Code02Factorial {
public static void main(String[] args) {
int n = 4;
factorial(n);
}
private static void factorial(int n) {
long sum = 0; //所以阶乘求和的数
long fac = 1; //当前数的阶乘
for (int i = 1; i <= n; i++) {
fac = fac * i;
sum = sum + fac;
}
System.out.println(n + "的阶乘之和是:" + sum);
}
}
运行效果