Problem Solving: Temperature Conversion
Learn to write a program that takes a specific temperature scale and converts it into a different temperature scale.
We'll cover the following
Temperature scales
There are three main temperature scales:
- Celsius
- Fahrenheit
- Kelvin
We can convert one temperature scale into another temperature scale by using conversion formulas. Look at the table below to see the relationships between the different temperature scales.
Temperature Conversion Formulas
Conversion | Formula |
Celsius to Fahrenheit | ° F = 9/5 ( ° C) + 32 |
Kelvin to Fahrenheit | ° F = 9/5 (K - 273) + 32 |
Fahrenheit to Celsius | ° C = 5/9 (° F - 32) |
Celsius to Kelvin | K = ° C + 273 |
Kelvin to Celsius | ° C = K - 273 |
Fahrenheit to Kelvin | K = 5/9 (° F - 32) + 273 |
Problem statement
Write a menu-based program in which we will take Celsius, Kelvin, and Fahrenheit as input and convert Celsius into Kelvin and Fahrenheit Kelvin into Celsius and Fahrenheit and Fahrenheit into Celsius and Kelvin.
Sample Program
Press 1 for Celsius to Fahrenheit
Press 2 for Celsius to Kelvin
Press 3 for Kelvin to Celsius
Press 4 for Kelvin to Fahrenheit
Press 5 for Fahrenheit to Celsius
Press 6 for Fahrenheit to Kelvin
1
________________________________
Converting Celsius to Fahrenheit
Enter Celsius Temperature: 100
Fahrenheit Temperature: 212 F
________________________________
To solve this problem, we will take input according to the menu. If the user presses 1
, we will take Celsius as input and convert it into Fahrenheit using the conversion formula. In the same way, we will follow other temperature conversion choices in the program.
Let us write the complete code below and run it to see the output.
#include<iostream> using namespace std; int main() { float celsius,Fahrenheit,kelvin; char option; cout << "Press 1 for Celsius to Fahrenheit" << endl; cout << "Press 2 for Celsius to Kelvin" << endl; cout << "Press 3 for Kelvin to Celsius" << endl; cout << "Press 4 for Kelvin to Fahrenheit" << endl; cout << "Press 5 for Fahrenheit to Celsius" << endl; cout << "Press 6 for Fahrenheit to Kelvin" << endl; cin >> option; // press any number from 1-6 for temperature conversion switch(option) { case '1': { cout << "Enter the temperature in Celsius: "; cin >> celsius; Fahrenheit = (1.8 * celsius) + 32.0; // Celsius to Fahrenheit conversion formula cout << "Fahrenheit:" << Fahrenheit << " F" << endl; break; } case '2': { cout << "Enter the temperature in Celsius: "; cin >> celsius; kelvin = celsius + 273; // Celsius to Kelvin conversion formula cout << "Kelvin:" << kelvin << " K" << endl; break; } case '3': { cout << "Enter the temperature in Kelvin: "; cin >> kelvin; celsius = kelvin - 273; // Kelvin to celsius conversion formula cout << "celcisus:" << kelvin << " C" << endl; break; } case '4': { cout << "Enter the temperature in Kelvin: "; cin >> kelvin; Fahrenheit = 1.8* (kelvin - 273) + 32;// Kelvin to Fahrenheit conversion formula cout << "Fahrenheit:" << kelvin << " F" << endl; break; } case '5': { cout << "Enter the temperature in Fahrenheit: "; cin >> Fahrenheit; celsius = (Fahrenheit - 32) / 1.8; // celsius to Fahrenheit conversion formula cout << "\nTemperature in degree Celsius: " << celsius << " C" << endl; break; } case '6': { cout << "Enter the temperature in Fahrenheit: "; cin >> Fahrenheit; kelvin = 0.5* (Fahrenheit- 32) + 273;// Fahrenheit to kelvin conversion formula cout << "\nTemperature in degree Kelvin: " << kelvin << " K" << endl; break; } default: cout << "Enter correct input" << endl; } return 0; }
- In line 16, we take input according to the menu base.
- In line 23, we convert Celsius to Fahrenheit using the conversion formula.
- In line 31, we convert Celsius to Kelvin using the conversion formula.
- In line 39, we convert Kelvin to Celsius using the conversion formula.
- In line 47, we convert Kelvin to Fahrenheit using the conversion formula.
- In line 55, we convert Celsius to Fahrenheit using the conversion formula.
- In line 63, we convert Fahrenheit to Kelvin using the conversion formula.
Quiz Temperature conversion
We have the input celsius=100
. In line 23, what happens if we replace Fahrenheit =(1.8 * celsius) + 32.0
with Fahrenheit =(9/5 * celsius) + 32.0
in code. What will be the output?
212 F
132 F
Exercise: handling invalid option
In the above playground, all options other than the options of '1'
,'2'
,…,'6'
are considered as invalid input. How can we modify the above program such that if the entered option is wrong, it should prompt for the input again (until the user enters the correct option).