从1加到8的阶层的和用java实现
时间: 2024-02-12 14:07:29 浏览: 143
以下是使用Java语言实现从1加到8的阶层的和的代码:
```
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 8; i++) {
int factorial = 1;
for (int j = 1; j <= i; j++) {
factorial *= j;
}
sum += factorial;
}
System.out.println("从1加到8的阶层的和为:" + sum);
}
}
```
运行结果为:
```
从1加到8的阶层的和为:41113
```
相关问题
用Java代码实现1到n的阶乘向加
public class FactorialSum {
public static void main(String[] args) {
int n = 5;
long sum = 0;
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
sum += factorial;
}
System.out.println("1到" + n + "的阶乘向加为:" + sum);
}
}
实现1的阶乘到100的阶乘的和Java
public class FactorialSum {
public static void main(String[] args) {
long sum = 0;
long factorial = 1;
for (int i = 1; i <= 100; i++) {
factorial *= i; // 计算 i 的阶乘
sum += factorial; // 累加阶乘的值
}
System.out.println("1! + 2! + 3! + ... + 100! = " + sum);
}
}
阅读全文