如何使用函式編寫溫度轉換表?


溫度轉換僅僅是把華氏溫度轉換成攝氏溫度或把攝氏溫度轉換成華氏溫度。

在這個程式設計中,我們將解釋如何將華氏溫度轉換成攝氏溫度,如何使用函式將轉換結果顯示成表格格式。

示例

以下是用於完成溫度轉換的 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。

2021 年 3 月 8 日更新

1K+ 瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.