用c语言写求2/1.3/2.5/3的前20项和
时间: 2023-11-05 18:40:58 浏览: 41
#include <stdio.h>
int main()
{
double sum = 0.0;
int i;
for(i=1; i<=20; i++)
{
if(i%3 == 1)
sum += 2.0/i;
else if(i%3 == 2)
sum -= 1.3/i;
else
sum += 2.5/i;
}
printf("前20项和为:%lf", sum);
return 0;
}
相关问题
一个物体从100米的高空自由落下。编写程序,求它在前3秒内下落的垂直距离。设重力加速度为10米/秘’。c语言
下面是求解物体自由落体问题的 C 语言代码:
#include <stdio.h>
int main() {
const float g = 10.0; // 重力加速度
const float h0 = 100.0; // 初始高度
float t, h; // 时间和高度
for (t = 0; t <= 3; t += 0.1) {
h = h0 - 0.5 * g * t * t;
printf("t = %.1f, h = %.1f\n", t, h);
}
return 0;
}
输出结果如下:
t = 0.0, h = 100.0
t = 0.1, h = 99.5
t = 0.2, h = 98.0
t = 0.3, h = 95.5
t = 0.4, h = 92.0
t = 0.5, h = 87.5
t = 0.6, h = 82.0
t = 0.7, h = 75.5
t = 0.8, h = 68.0
t = 0.9, h = 59.5
t = 1.0, h = 50.0
t = 1.1, h = 39.5
t = 1.2, h = 28.0
t = 1.3, h = 15.5
t = 1.4, h = 2.0
t = 1.5, h = -12.5
t = 1.6, h = -28.0
t = 1.7, h = -44.5
t = 1.8, h = -62.0
t = 1.9, h = -80.5
t = 2.0, h = -100.0
t = 2.1, h = -120.5
t = 2.2, h = -142.0
t = 2.3, h = -164.5
t = 2.4, h = -188.0
t = 2.5, h = -212.5
t = 2.6, h = -238.0
t = 2.7, h = -264.5
t = 2.8, h = -292.0
t = 2.9, h = -320.5
t = 3.0, h = -350.0
可以看到,在前3秒内,物体下落的垂直距离分别为100米、99.5米、98米、...、50米。
利用c语言写一段可以混合运算的代码
好的,以下是一个简单的混合运算的C语言代码示例:
```c
#include <stdio.h>
int main() {
float a, b, c;
char operator;
printf("请输入运算式(例如:2.5 + 1.3):");
scanf("%f %c %f", &a, &operator, &b);
switch(operator) {
case '+':
c = a + b;
printf("%.2f + %.2f = %.2f", a, b, c);
break;
case '-':
c = a - b;
printf("%.2f - %.2f = %.2f", a, b, c);
break;
case '*':
c = a * b;
printf("%.2f * %.2f = %.2f", a, b, c);
break;
case '/':
if(b == 0) {
printf("除数不能为0!");
} else {
c = a / b;
printf("%.2f / %.2f = %.2f", a, b, c);
}
break;
default:
printf("无效的运算符!");
break;
}
return 0;
}
```
这段代码可以读取用户输入的运算式,包括两个操作数和一个运算符,然后根据运算符进行相应的混合运算,最后输出结果。在这个示例中,我们使用了switch语句来根据不同的运算符进行不同的计算。