编写程序求(即求1!+2!+3!+4!+…+20!)
时间: 2023-11-21 09:55:28 浏览: 89
MoreEffectiveC++
以下是两种编写程序求1!+2!+3!+4!+…+20!的方法:
方法1:
```c++
#include <stdio.h>
#include <stdlib.h>
int main() {
int sum = 0;
for (int i = 1; i <= 20; i++) {
int ret = 1;
for (int j = 1; j <= i; j++) {
ret = j * ret;
}
sum = ret + sum;
}
printf("%d", sum);
system("pause");
return 0;
}
```
方法2:
```c++
#include <stdio.h>
#include <stdlib.h>
int main() {
int ret = 1;
int sum = 0;
for (int i = 1; i <= 20; i++) {
ret = i * ret;
sum = sum + ret;
}
printf("%d", sum);
system("pause");
return 0;
}
```
阅读全文