Program for Fahrenheit to Kelvin conversion in C++

C++Server Side ProgrammingProgramming

Given with temperature ‘n’ in Fahrenheit and the challenge is to convert the given temperature to kelvin and display it.

Example

Input 1-: 549.96
Output -: Temperature in fahrenheit 549.96 to kelvin : 561.256
Input 2-: 23.45
Output -: Temperature in fahrenheit 23.45 to kelvin : 268.4

For converting the temperature from Fahrenheit to Kelvin, there is a formula which is given below

K = 273.5 + ((F - 32.0) * (5.0/9.0))

Where, K is temperature in kelvin and F is temperature in Fahrenheit

Approach used below is as follows

  • Input temperature in a float variable let’s say Fahrenheit
  • Apply the formula to convert the temperature into Kelvin
  • Print kelvin

Algorithm

Start
Step 1-> Declare function to convert Fahrenheit to kelvin
   float convert(float fahrenheit)
   return 273.5 + ((fahrenheit - 32.0) * (5.0/9.0))
step 2-> In main()
   declare and set float fahrenheit = 549.96
   Call convert(fahrenheit)
Stop

Example

 Live Demo

#include<iostream>
using namespace std ;
//convert fahrenheit to kelvin
float convert(float fahrenheit) {
   return 273.5 + ((fahrenheit - 32.0) * (5.0/9.0));
}
int main() {
   float fahrenheit = 549.96;
   cout << "Temperature in fahrenheit "<<fahrenheit<<" to kelvin : "<<convert(fahrenheit) ;
   return 0;
}

Output

IF WE RUN THE ABOVE CODE IT WILL GENERATE FOLLOWING OUTPUT

Temperature in fahrenheit 549.96 to kelvin : 561.256
raja
Updated on 18-Oct-2019 08:29:07

Advertisements