C++ 中攝氏度轉換為華氏度的程式
給出攝氏度的溫度 'n',挑戰是將給定的溫度轉換為華氏度並顯示它。
比如
Input 1-: 100.00 Output -: 212.00 Input 2-: -40 Output-: -40
從攝氏度轉換為華氏度的溫度有以下公式
T(°F) = T(°C) × 9/5 + 32
其中,T(°C) 是攝氏度溫度,T(°F) 是華氏度溫度
下面使用的演算法如下
- 輸入浮點變數中的溫度,記為攝氏度
- 應用公式將溫度轉換為華氏度
- 列印華氏度
演算法
Start Step 1 -> Declare a function to convert Celsius to Fahrenheit void cal(float cel) use formula float fahr = (cel * 9 / 5) + 32 print cel fahr Step 2 -> In main() Declare variable as float Celsius Call function cal(Celsius) Stop
使用 C 語言
比如
#include <stdio.h>
//convert Celsius to fahrenheit
void cal(float cel){
float fahr = (cel * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit", cel, fahr);
}
int main(){
float Celsius=100.00;
cal(Celsius);
return 0;
}輸出
100.00 Celsius = 212.00 Fahrenheit
使用 C++ 語言
比如
#include <bits/stdc++.h>
using namespace std;
float cel(float n){
return ((n * 9.0 / 5.0) + 32.0);
}
int main(){
float n = 20.0;
cout << cel(n);
return 0;
}輸出
68
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP