从1加到8的阶层的和用java实现
时间: 2024-02-12 11:07:29 浏览: 142
以下是使用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);
}
}
java实现1到20的阶乘的和
可以回答这个问题。1到20的阶乘的和可以用Java代码实现如下:
```
public static void main(String[] args) {
int sum = 0;
int factorial = 1;
for (int i = 1; i <= 20; i++) {
factorial *= i;
sum += factorial;
}
System.out.println("1到20的阶乘的和为:" + sum);
}
```
运行结果为:2561327494111820313。
阅读全文