如何使用函式編寫溫度轉換表?
溫度轉換僅僅是把華氏溫度轉換成攝氏溫度或把攝氏溫度轉換成華氏溫度。
在這個程式設計中,我們將解釋如何將華氏溫度轉換成攝氏溫度,如何使用函式將轉換結果顯示成表格格式。
示例
以下是用於完成溫度轉換的 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