C++ Code to Convert Celsius to Fahrenheit (vice versa).

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;                    
}
Output:
Enter the temperature in Celsius: 50
The temperature in Fahrenheit is: 122

Converting Fahrenheit to Celsius.
We can also convert Fahrenheit to Celsius by deriving a new formula using the same above formula. We just did some basic math to create Fahrenheit to Celsius formula as shown below.

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

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS