C++ | Temperature conversion

Introduction

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. Go enjoy the program. Lets begin………

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 it work

  1. You enter the Temperature in given unit and is saved in variable ‘a’.
  2. The Temperature is converted according to conversion formula and is saved in ‘b’.
  3. Variable ‘b’ is shown by cout.

Extending it

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

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as float (name them as you want.)
  5. Add cout and cin of variables you want to convert from.(as shown in program)
  6. Convert the Temperature from variable and save them in one variable.
  7. 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.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s