ด้วยอุณหภูมิ 'n' ในเซลเซียส ความท้าทายคือการแปลงอุณหภูมิที่กำหนดเป็นฟาเรนไฮต์และแสดงผล
ตัวอย่าง
Input 1-: 100.00 Output -: 212.00 Input 2-: -40 Output-: -40
สำหรับการแปลงอุณหภูมิจากเซลเซียสเป็นฟาเรนไฮต์นั้นมีสูตรดังนี้
T(°F) =T(°C) × 9/5 + 32
โดยที่ T(°C) คืออุณหภูมิในหน่วยเซลเซียส และ T(°F) คืออุณหภูมิในหน่วยฟาเรนไฮต์
แนวทางที่ใช้ด้านล่างมีดังนี้
- อุณหภูมิอินพุตในตัวแปร float สมมุติว่าเซลเซียส
- ใช้สูตรแปลงอุณหภูมิเป็นฟาเรนไฮต์
- พิมพ์ฟาเรนไฮต์
อัลกอริทึม
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
การใช้ภาษาซี
ตัวอย่าง
#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