In this post, we will learn to write C++ code to convert Celsius to Fahrenheit. We can use below conversion formula for our temperature calculation. Let's understand with one example.
Formula to calculate Fahrenheit when Celsius is given:
F = (C * 1.8) + 32
In the above mathematical formula, C stands for temperature given in Degree Celsius and F stands for the Degree Fahrenheit temperature we will get after the calculation. In most countries, Celsius is used to measuring the temperature but in US Fahrenheit is used to measure temperature.
Example: Find the degree Fahrenheit when the celsius temperature is 50°C.
F = (C * 1.8) + 32
= (50 * 1.8) + 32
= (90) + 32
= 122°F(alert-passed)
C++ Code Implementation for Celsius to Fahrenheit conversion.
//C++ code to convert celsius to Fahrenheit #include<iostream> using namespace std; int main(){ float celsius, fahrenheit; cout<<"Enter the temperature in Celsius: ";
cin>>celsius; fahrenheit = (celsius * 1.8) + 32; cout<<"The temperature in Fahrenheit is: "<<fahrenheit; return 0; }
Enter the temperature in Celsius: 50 The temperature in Fahrenheit is: 122
F = (C * 1.8) + 32
F - 32 = (C * 1.8)
(F - 32)/1.8 = C
C = (F - 32)/1.8
Example: Find the degree Celsius when the Fahrenheit temperature is 122°F.
C = (F - 32)/1.8
= (122 - 32)/1.8
= (90)/1.8
= 50°F(alert-passed)
C++ Code Implementation for Fahrenheit to Celsius Conversion.
//C++ code to convert Fahrenheit to celsius #include<iostream> using namespace std; int main(){ float celsius, fahrenheit; cout<<"Enter the temperature in Fahrenheit: ";
cin>>fahrenheit; celsius = (fahrenheit - 32) / 1.8; cout<<"The temperature in Celsius is: "<<celsius; return 0; }
Output:
Enter the temperature in Fahrenheit: 122 The temperature in Celsius is: 50
No comments:
Post a Comment