计算sum+=+1!+++2!+++3!+++...+++(n-1)!+++n!
时间: 2024-01-11 10:21:49 浏览: 77
pat1-12题通过的代码
以下是两种计算sum的方法:
1. 使用while循环来计算:
```python
n = int(input())
jie = 1
sum = 0
i = 1
while n >= i:
jie = jie * i
sum = sum + jie
i = i + 1
print(sum)
```
2. 使用for循环来计算:
```c
#include <stdio.h>
int main(void) {
int n = 0;
int i = 0;
int m = 1;
int sum=0;
printf("请输入n的值:");
scanf("%d", &n);
for (i=1; i<=n; ++i) {
m = m * i;
sum = sum +m;
}
printf ("sum = %d\n", sum);
return 0;
}
```
阅读全文