计算s=2/1+3/2+5/3+8/5+...的前10项之和C语言程序
时间: 2023-11-18 11:52:03 浏览: 79
以下是计算s=2/1+3/2+5/3+8/5+...的前10项之和的C语言程序:
```
#include <stdio.h>
int main() {
float n=2, m=1, sum=2, t;
int i;
for(i=2; i<=10; i++){
t = n;
n += m;
m = t;
sum += n/m;
}
printf("前10项之和为:%f\n", sum);
return 0;
}
```
相关问题
c语言3.+编写程序计算下列S的值。 (1)+S=1-1/2+1/3-1/4+1/5+……-1/100 (2)+S=1!+2!+3!+……10! (3)+S=1/2+2/3+3/5+5/8+……++(
以下是三个C语言程序,分别计算了三个不同的数学公式的值:
1.计算1-1/2+1/3-1/4+1/5+……-1/100的值
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 100;
double sum = 0;
int flag = 1;
for (int i = 1; i <= n; i++)
{
sum += flag * 1.0 / i;
flag = -flag;
}
printf("%.4lf", sum);
return 0;
}
```
2.计算1!+2!+3!+……10!的值
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 10;
int sum = 0;
int fac = 1;
for (int i = 1; i <= n; i++)
{
fac *= i;
sum += fac;
}
printf("%d", sum);
return 0;
}
```
3.计算1/2+2/3+3/5+5/8+……的值
```c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 20;
double sum = 0;
int a = 1, b = 2;
for (int i = 1; i <= n; i++)
{
sum += (double)a / b;
int temp = b;
b = a + b;
a = temp;
}
printf("%.4lf", sum);
return 0;
}
```
用c语言写程序,已知 s=1/2+2/3+3/5+5/8+...,求 前20 项的和
以下是用C语言编写的程序,用于计算s=1/2+2/3+3/5+5/8+...前20项的和:
```c
#include <stdio.h>
int main()
{
double sum = 0.0f;
double a = 1.0f, b = 2.0f, c;
int i;
for (i = 1; i <= 20; i++)
{
sum += a / b;
c = a + b;
a = b;
b = c;
}
printf("s=%.3f", sum);
return 0;
}
```
阅读全文