1!+2!+3!+4!+……+n! n=20 (n为奇数) c语言
时间: 2023-11-24 19:52:34 浏览: 110
以下是使用C语言计算1!+2!+3!+4!+……+n!的程序:
```c
#include <stdio.h>
int factorial(int num) {
int result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}
int main() {
int n;
int sum = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
sum += factorial(i);
}
}
printf("%d\n", sum);
return 0;
}
```
相关问题
c语言1!+2!+3!+4!+……+n! n=20 (n为奇数)
以下是C语言计算1!+2!+3!+4!+……+n!的代码,其中n=20且为奇数:
```c
#include <stdio.h>
int main()
{
int n = 20;
int sum = 0;
int factorial = 1;
for (int i = 1; i <= n; i++)
{
factorial *= i;
if (i % 2 == 1)
{
sum += factorial;
}
}
printf("%d\n", sum);
return 0;
}
```
(1)1/3+3/5+5/7+……+n/(n+2) //n为任意一个奇数 (2)1!+2!+3!+4!+……+n! n=20 (n为奇数) 用c语言
以下是两个用C语言实现的例子:
(1)1/3+3/5+5/7+……+n/(n+2) //n为任意一个奇数
```c
#include <stdio.h>
int main() {
int n;
float sum = 0;
printf("请输入一个奇数n:");
scanf("%d", &n);
for (int i = 1; i <= n; i += 2) {
sum += (float)i / (i + 2);
}
printf("1/3+3/5+5/7+……+n/(n+2)的和为:%f", sum);
return 0;
}
```
(2)1!+2!+3!+4!+……+n! n=20(n为奇数)
```c
#include <stdio.h>
int main() {
int n = 20;
long long sum = 0, factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
if (i % 2 == 1) {
sum += factorial;
}
}
printf("1!+2!+3!+4!+……+n!的和为:%lld", sum);
return 0;
}
```
阅读全文