请写出与以下for语句等价的while语句。 for (fahr = lower; fahr<= upper; fahr++){ celsius = (5.0 / 9.0) * (fahr - 32); printf("%4d%6.1f\n", fahr, celsius); }
时间: 2024-03-25 19:40:23 浏览: 91
以下是与给定for语句等价的while语句:
```c++
fahr = lower;
while (fahr <= upper) {
celsius = (5.0 / 9.0) * (fahr - 32);
printf("%4d%6.1f\n", fahr, celsius);
fahr++;
}
```
这段while循环的实现与for循环的实现等价,都是对温度变量fahr从lower到upper进行遍历,计算对应的摄氏温度并输出。在while循环中,变量fahr的初始值为lower,每次循环时先执行计算和输出操作,再将fahr加1,直到fahr的值大于upper时退出循环。
相关问题
请写出与以下for语句等价的while语句。 for (fahr = Lower; fahr<= upper; fahr++){ celsius = (5.日 / 9.0) * (fahr - 32); printf("%40%6.1fIn”, fahr, celsius);
下面是与给定for循环等价的while循环语句:
```c
fahr = Lower;
while (fahr <= upper) {
celsius = (5.0 / 9.0) * (fahr - 32);
printf("%40%6.1fIn", fahr, celsius);
fahr++;
}
```
解释:将变量fahr初始化为Lower,然后检查fahr是否小于或等于upper。如果是,则计算摄氏温度并打印输出。最后,将fahr增加1,以使下一次循环中的fahr值不同。重复这个过程,直到fahr的值大于upper为止。
编写一个c语言程序,求摄氏温度,要求输出fahr=100和celsius为整数
以下是我作为 CSDN 开发的 C 知道的回答:
#include <stdio.h>
int main() {
int fahr, celsius;
int lower, upper, step;
lower = ; /* 温度表的下限 */
upper = 300; /* 温度表的上限 */
step = 20; /* 步长 */
celsius = lower;
while (celsius <= upper) {
fahr = celsius * 9 / 5 + 32;
printf("celsius=%d, fahr=%d\n", celsius, fahr);
celsius = celsius + step;
}
printf("fahr=%d, celsius=%d\n", 100, (100 - 32) * 5 / 9);
return ;
}
输出结果为:
celsius=, fahr=32
celsius=20, fahr=68
celsius=40, fahr=104
celsius=60, fahr=140
celsius=80, fahr=176
celsius=100, fahr=212
celsius=120, fahr=248
celsius=140, fahr=284
celsius=160, fahr=320
celsius=180, fahr=356
celsius=200, fahr=392
celsius=220, fahr=428
celsius=240, fahr=464
celsius=260, fahr=500
celsius=280, fahr=536
celsius=300, fahr=572
fahr=100, celsius=37
希望能对您有所帮助。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)