c++ program to convert fahrenheit to kelvin
#include <iostream>
using namespace std;
double convertFahrenheitToKelvin(double fahrenheit) {
double kelvin = (fahrenheit + 459.67) * 5 / 9;
return kelvin;
}
int main() {
double fahrenheit;
cout << "Enter temperature in Fahrenheit: ";
cin >> fahrenheit;
double kelvin = convertFahrenheitToKelvin(fahrenheit);
cout << "Temperature in Kelvin: " << kelvin << endl;
return 0;
}
The first step is to include the necessary header file
iostream
which allows us to use the input/output stream functionality in C++.We declare the
using namespace std;
statement to avoid having to prependstd::
to all the standard library functions we will be using.Next, we define a function
convertFahrenheitToKelvin
that takes in a parameterfahrenheit
of typedouble
. This function will convert the given Fahrenheit temperature to Kelvin and return the result.Inside the
convertFahrenheitToKelvin
function, we calculate the value of Kelvin using the formula(fahrenheit + 459.67) * 5 / 9
and store it in the variablekelvin
.The
convertFahrenheitToKelvin
function then returns the value ofkelvin
.In the
main
function, we declare a variablefahrenheit
of typedouble
to store the user input for the Fahrenheit temperature.We prompt the user to enter the temperature in Fahrenheit using the
cout
statement and<<
operator.We read the user input from the console and store it in the variable
fahrenheit
using thecin
statement and>>
operator.Next, we call the
convertFahrenheitToKelvin
function with thefahrenheit
variable as an argument and store the returned value in the variablekelvin
.Finally, we output the converted temperature in Kelvin using the
cout
statement and<<
operator.The
main
function returns 0 to indicate successful execution of the program.