用c语言求 1!+2!+3!+4!+5!+…+15!
时间: 2023-11-17 08:02:15 浏览: 84
51单片机(STC15W4K56S4)麦克纳姆轮全向智能小车PS2手柄控制+灭火程序源代码.rar
可以使用for循环或者while循环来实现1!+2!+3!+4!+5!+…+15!的累加。具体代码如下:
方法一:使用for循环
```c
#include <stdio.h>
int main()
{
int i, n;
long long sum = 0, factorial = 1;
printf("请输入N的值:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
factorial *= i;
sum += factorial;
}
printf("1!+2!+3!+...+%d! = %lld\n", n, sum);
return 0;
}
```
方法二:使用while循环
```c
#include <stdio.h>
int main()
{
int i = 1, n;
long long sum = 0, factorial = 1;
printf("请输入N的值:");
scanf("%d", &n);
while (i <= n) {
factorial *= i;
sum += factorial;
i++;
}
printf("1!+2!+3!+...+%d! = %lld\n", n, sum);
return 0;
}
```
阅读全文