如何使用函数编写温度转换表?
温度转换仅仅是把华氏温度转换成摄氏温度或把摄氏温度转换成华氏温度。
在这个编程中,我们将解释如何将华氏温度转换成摄氏温度,如何使用函数将转换结果显示成表格格式。
示例
以下是用于完成温度转换的 C 程序 −
#include<stdio.h>
float conversion(float);
int main(){
float fh,cl;
int begin=0,stop=300;
printf("Fahrenheit \t Celsius
");// display conversion table heading
printf("----------\t-----------
");
fh=begin;
while(fh<=stop){
cl=conversion(fh); //calling function
printf("%3.0f\t\t%6.lf
",fh,cl);
fh=fh+20;
}
return 0;
}
float conversion(float fh) //called function{
float cl;
cl= (fh - 32) * 5 / 9;
return cl;
}输出
执行上述程序后,将产生以下结果 −
Fahrenheit Celsius ---------- ----------- 0 -18 20 -7 40 4 60 16 80 27 100 38 120 49 140 60 160 71 180 82 200 93 220 104 240 116 260 127 280 138 300 149
类似地,你可以编写程序来完成摄氏温度到华氏温度的转换
只需将公式更改为:
华氏 = (摄氏 * 9 / 5) + 32。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP