# Program for temperature conversion in C++

In this tutorial, we will learn to **convert temperature from Celsius to Fahrenheit and vice versa in C++**. Many times, there is a requirement to convert the temperature measurement from Celsius to Fahrenheit or vice versa. Also, in the medical field, there is a need to convert the temperature measurement scale. So, here we will learn to convert the temperature measurement scale from Celsius to Fahrenheit and vice versa. Also, if you want a C++ program for temperature conversion you are in the right place.

## Temperature conversion from Celsius to Fahrenheit in C++

So, to convert the temperature from Celsius to Fahrenheit, we will use the formula given below –

*F = ( (C * 9.0) / 5.0 ) + 32*

where,

F is the temperature in Fahrenheit

C is the temperature in Celsius

For example –

If the temperature in Celsius is C = 37 degree then,

The equivalent temperature in Fahrenheit will be F = ( (37 * 9) / 5 ) + 32 = 98.6 degree

## Temperature conversion from Fahrenheit to Celsius

So, to convert the temperature from Fahrenheit to Celsius, we will use the formula given below –

**C = 5.0 * (F – 32) / 9.0**

where,

C is the temperature in Celsius

F is the temperature in Fahrenheit

For example –

If the temperature in Fahrenheit is F = 98.6 degree then,

The equivalent temperature in Celsius will be C = 5 * ( 98.6 – 32 ) / 9 = 37 degree

## Program for temperature conversion in C++

In this program, we will take the temperature in Celsius and display the temperature in Fahrenheit after conversion and vice versa. The temperature from Celsius to Fahrenheit and Fahrenheit to Celsius is converted using the above formulae. So, the program for temperature conversion in C++ is –

#include<iostream> using namespace std; float c_f(float); float f_c(float); int main() { float temp; cout<<"\nENTER TEMPERATURE IN CELSIUS : "; cin>>temp; cout<<"TEMPERATURE IN FAHRENHEIT IS : "<<c_f(temp); cout<<"\n\nENTER TEMPERATURE IN FAHRENHEIT : "; cin>>temp; cout<<"TEMPERATURE IN CELSIUS IS : "<<f_c(temp); return 0; } float c_f(float celsius) { float fahrenheit=(celsius*9.0)/5.0+32; return fahrenheit; } float f_c(float fahrenheit) { float celsius=5.0*(fahrenheit-32)/9.0; return celsius; }

There are two functions in the program –

**c_f() function –**This function returns the result after converting the temperature from Celsius to Fahrenheit.**f_c() function –**This function returns the result after converting the temperature from Fahrenheit to Celsius.

Firstly, the program takes the temperature in Celsius as input in a variable of data type float. Then displays the equivalent temperature in Fahrenheit. Again, it takes the temperature in Fahrenheit and displays the equivalent temperature in Celsius.

#### The output of the above program

The program gives the equivalent temperature after conversion. The output of the above program during execution is as follows –

[email protected]:~/cpp$ g++ temperature.cpp [email protected]:~/cpp$ ./a.out ENTER TEMPERATURE IN CELSIUS : -40 TEMPERATURE IN FAHRENHEIT IS : -40 ENTER TEMPERATURE IN FAHRENHEIT : -40 TEMPERATURE IN CELSIUS IS : -40 [email protected]:~/cpp$ ./a.out ENTER TEMPERATURE IN CELSIUS : 37 TEMPERATURE IN FAHRENHEIT IS : 98.6 ENTER TEMPERATURE IN FAHRENHEIT : 108 TEMPERATURE IN CELSIUS IS : 42.2222 [email protected]:~/cpp$

The temperature in Celsius and Fahrenheit is the same at -40 degrees. The temperature in Celsius entered by the user is 37 degrees and its equivalent temperature in Fahrenheit calculated by the program is 98.6 degrees. And the temperature in Fahrenheit entered by the user is 108 which is equivalent to 42.2 degrees in Celsius.

Thank you for reading this tutorial. I hope it helps you.

Also read: Convert RGB to Hex color code in C++

## Leave a Reply