This program will let you convert temperature from one unit to other unit. The first program will convert Celsius to Kelvin. The second will convert Kelvin to Celsius. The next will convert Celsius to Fahrenheit. The last one will convert Fahrenheit. So you can make more programs by using more different types of unit and their conversion.
Program to convert Celsius to Kelvin
#include<iostream.h> #include<conio.h> void main() { //clear the screen. clrscr(); //declare variable type float float a,b; //Input the Temperature in given unit save them in ‘a’ cout<<”Enter the Temperature in Celsius”<<endl; cin>>a; //convert and save it in ‘b’ b=a+273; //show the output ‘b’ cout<<”Temperature in Kelvin is “<<b; //get character getch(); }
Output
Enter the Temperature in Celsius
27.5
Temperature in Kelvin is 300.5
Program to convert Kelvin to Celsius
#include<iostream.h> #include<conio.h> void main() { //clear the screen. clrscr(); //declare variable type float float a,b; //Input the Temperature in given unit save them in ‘a’ cout<<”Enter the Temperature in Kelvin”<<endl; cin>>a; //convert and save it in ‘b’ b=a-273; //show the output ‘b’ cout<<”Temperature in Celcius is “<<b; //get character getch(); }
Output
Enter the Temperature in Celsius
300.5
Temperature in Kelvin is 27.5
Program to convert Celsius to Fahrenheit
#include<iostream.h> #include<conio.h> void main() { //clear the screen. clrscr(); //declare variable type float float a,b; //Input the Temperature in given unit save them in ‘a’ cout<<”Enter the Temperature in Celsius”<<endl; cin>>a; //convert and save it in ‘b’ b=1.8*a+32.0; //show the output ‘b’ cout<<”Temperature in Fahrenheit is “<<b; //get character getch(); }
Output
Enter the Temperature in Celsius
56
Temperature in Fahrenheit is 132.800003
Program to convert Fahrenheit to Celsius
#include<iostream.h> #include<conio.h> void main() { //clear the screen. clrscr(); //declare variable type float float a,b; //Input the Temperature in given unit save them in ‘a’ cout<<”Enter the Temperature in Fahrenheit”<<endl; cin>>a; //convert and save it in ‘b’ b=(a-32.0)/1.8; //show the output ‘b’ cout<<”Temperature in Celsius is “<<b; //get character getch(); }
Output
Enter the Temperature in Fahrenheit
132.800003
Temperature in Celsius is 56.0
How does Temperature Conversion works in C++?
- You enter the Temperature in given unit and is saved in variable ‘a’.
- The Temperature is converted according to conversion formula and is saved in ‘b’.
- Variable ‘b’ is shown by cout.
Extending Unit of Temperature Conversion with C++ Codes
The Program can be extended by adding more unit to conversion and by adding their Conversion formula for other unit and more variables can be added to program to convert multiple temperatures. Like wise you can also use following unit for conversion :-
- Kelvin
- Celsius
- Fahrenheit
Explanation of C++ Program to Preform Temperature Conversion
- Include ‘iostream.h’ and ‘conio.h’ files.
- Add void main.
- Start program by first clearing the screen.
- Declare the variables as float (name them as you want.)
- Add cout and cin of variables you want to convert from.(as shown in program)
- Convert the Temperature from variable and save them in one variable.
- Cout the converted Temperature variable.
At the end
You learnt creating the c++ program of conversion of Temperature from one unit to other unit. So now enjoy the program.
You can follow us on Twitter, add us to your circle on Google+ or like our Facebook page . We will be providing you with best online tutorial on Programming, Database, Data Structures, Engineering, Case Studies and Computer Fundamentals.
The post C++ Program on Temperature Conversion ‘Celsius to Kelvin to Fahrenheit’ appeared first on The Computer Students.