用C语言编写一个可以求出S=1-1/(!+2!)+1/(1!+2!+3!)-···-1/(1!+2!+···+10!)的程序
时间: 2023-11-26 16:05:59 浏览: 32
以下是用C语言编写一个可以求出S=1-1/(!+2!)+1/(1!+2!+3!)-···-1/(1!+2!+···+10!)的程序:
```c
#include <stdio.h>
int main()
{
int i, j, n;
float s, t, k;
s = 0;
for (i = 1; i <= 10; i++)
{
t = 1;
k = 1;
for (j = 1; j <= i; j++)
{
k = k * j;
t = t + k;
}
n = i + 1;
if (n % 2 == 0)
{
s = s - 1 / t;
}
else
{
s = s + 1 / t;
}
}
printf("S=%f\n", s);
return 0;
}
```
相关问题
编写一个程序计算S=1-1/2+1/3+1/m用c语言
以下是一个用C语言编写的计算S=1-1/2+1/3+1/m的程序:
```c
#include <stdio.h>
int main() {
int m;
float s = 0;
printf("请输入m的值:");
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
if (i % 2 == 0) {
s -= 1.0 / i;
} else {
s += 1.0 / i;
}
}
printf("S的值为:%f", s);
return 0;
}
```
用户需要输入m的值,程序会根据用户输入的m计算S的值并输出。在循环中,如果i是偶数,则减去1/i,否则加上1/i。
用C语言写一个计算s=1-2!+3!-4!+…+n!程序
以下是用C语言编写计算s=1-2!+3!-4!+…+n!的程序:
```c
#include <stdio.h>
int main() {
int n, i, j, sign = 1;
long long fact = 1, sum = 0;
printf("请输入一个整数n:");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
fact *= i;
if (i % 2 == 0) {
sign = -1;
} else {
sign = 1;
}
sum += sign * fact;
}
printf("s=%lld\n", sum);
return 0;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)