Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

โปรแกรมแปลงอุณหภูมิ Celsius เป็น Fahrenheit ใน C++


ให้อุณหภูมิ 'n' ใน Celsius แปลงเป็น Fahrenheit และแสดงผล

ตัวอย่าง

Input 1-: 100.00   Output -: 212.00 Input 2-: -40   Output-: -40

สูตรแปลง −

T(°F) = T(°C) × 9/5 + 32

โดย T(°C) คืออุณหภูมิ Celsius T(°F) คืออุณหภูมิ Fahrenheit

วิธีแก้

  • รับอุณหภูมิในตัวแปร float เช่น Celsius
  • ใช้สูตรแปลงเป็น Fahrenheit
  • พิมพ์ Fahrenheit

ALGORITHM

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

Using 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

Using 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