Temperature Conversion Program Using C++ Programming Language

How to convert temperature from one form to another form using C++ programming language.There are different forms of temperature in which the temperature is measured in daily life. Mostly, the temperature is measured in Celsius form.

Forms:
  • Celsius
  • Fahrenheit
  • Kelvin

Temperature conversion can be done in the following categories.
  • Celsius to Fahrenheit
  • Celsius to Kelvin
  • Fahrenheit to Celsius 
  • Fahrenheit to Kelvin
  • Kelvin to Celsius
  • Kelvin to Fahrenheit
There are mathematical formulas in order to convert temperatures from one form to another form.These formulas are very easy to use , all just is to know the value of temperature , put it into the formula of that you want it to convert into that form.

Formulas:
  • Celsius to Fahrenheit:                 T (f)   = {  9 / 5 * T (c)  +  32  }
  • Celsius to Kelvin      :                 T (k)  = {  T (c)  + 273  }
  • Fahrenheit to Celsius:                 T (c)  = {  5 / 9  (  T (f) - 32 )  }
  • Fahrenheit to Kelvin :                 T (k)  = {  5 / 9  (  T (f) - 32 )  + 273 }
  • Kelvin to Celsius      :                 T (c)  = {  T (k)  - 273  }
  • Kelvin to Fahrenheit :                 T (f)   = {  9 / 5 ( T (k) - 273 ) + 32  }

Notations:
  • T (c)     ==>   temperature in Celsius
  • T (f)     ==>   temperature in Fahrenheit
  • T (k)    ==>   temperature in Kelvin

Now the important thing is how to implement these formulas using C++ Programming language.The answer is very simple just take care of mathematical rules while applying these formulas in your code. By mathematical rules , it means addition , multiplication , division , subtraction , brackets should be written in code correctly in such a way that if there is multiplication , then program doesn't go for addition or something else. In order to remember this just follow this word BoDMAS.

BODMAS:
  • B   ==>  Brackets
  • D   ==>  Division
  • M  ==>  Multiplication
  • A  ==>   Addition
  • S  ==>  Subtraction
Simply follow the rule , if there is expression, then first of all expression inside the brackets should solve, then division, then multiplication, then addition, then subtraction.Now the question is if there are lot of brackets, then solve the innermost bracket first. Now we should go for how to write conversion program for temperature using C++ programming language.

Program Code:
The program i am going to discuss basically convert temperature from Celsius to Fahrenheit and Fahrenheit to Celsius.Because if you understand this program then you can easily write the full temperature conversion program.The main thing is to get the concept. Once you get this , then it is very easy for you to any program using C++ programming.
  •  Celsius to Fahrenheit and
  •  Fahrenheit to Celsius.
First how the compiler knows that the user will enter the temperature is in Celsius or in Fahrenheit. I force the user to choose the conversion i.e if user want to convert temperature from Celsius to Fahrenheit then enter 1 , otherwise for second case enter 2. For this i have to define variable and of course the data type is of integer type. 

int choose;

After selecting the conversion, the user will allow to enter the  temperature , as there are two conversion cases in my program , so there must be two variables of FLOAT data type to handles these temperature cases. Now why float, because temperature may be in points form e.g 98.6.

float ctemp , ftemp;

Now, as there are two cases then how the compiler knows that i have to go for this conversion formula case.
This is done from the user , when user select the case by entering 1 or 2.
You can use if else statements or switch statements for program code. Let go for if else.

Program Code:
Note:  Click on the images to enlarge them.



 Output:  after compiling and executing , the output is

First it asked to choose between the two cases, i wrote 1 means, selected first conversion case.



Second ,after pressing enter, it asked me write temperature in Celsius as i selected this conversion in the first step , i wrote 100.


Third , after pressing enter , it showed the result in Fahrenheit , as i chose the conversion from Celsius to Fahrenheit.




Hope that you will find this helpful.




No comments:

Post a Comment

Please disable your ad blocker to support this website.

Our website relies on revenue from ads to keep providing free content.