用C语言帮我写一个求1!+3!+5!+….…+ n!
时间: 2023-10-08 21:07:11 浏览: 31
#include <stdio.h>
int main() {
int n, i, fact = 1;
printf("请输入n的值:");
scanf("%d", &n);
for (i = 1; i <= n; i += 2) {
fact *= i;
}
printf("1! 3! 5! ... %d! 的乘积为:%d\n", n, fact);
return 0;
}
相关问题
c语言求1!+2!+3!+...+n!
以下是C语言求1!+2!+3!+...+n!的代码:
```c
#include <stdio.h>
int main() {
int n;
while (scanf("%d", &n) != EOF) {
long long sum = 0, num = 1;
for (int i = 1; i <= n; i++) {
num *= i;
sum += num;
}
printf("%lld\n", sum);
}
return 0;
}
```
该代码使用了for循环来计算1!+2!+3!+...+n!,其中num表示当前的阶乘,sum表示前面所有阶乘的和。在每次循环中,num乘以i,然后加到sum中。最后输出sum即可。
c语言编程求 1!+2!+3!+...+N!
这个C语言程序可以求1!+2!+3!+...+N!的值。其中N是用户输入的值。程序使用了for循环和累加器来计算阶乘的和。具体实现过程如下:
1. 首先定义变量n、i、m和sum,分别表示用户输入的值、循环计数器、阶乘和和累加器。
2. 使用scanf函数获取用户输入的值n。
3. 使用for循环从1到n遍历每个数字,计算出当前数字的阶乘m,并将其加入到累加器sum中。
4. 循环结束后,使用printf函数输出累加器sum的值。
代码实现如下:
```
#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("1!+2!+3!+...+N!的值为:%d\n", sum);
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)