C++ Fahrenheit to Kelvin
Code Explanation: C++ Fahrenheit to Kelvin Conversion
#include <iostream>
using namespace std;
double fahrenheitToKelvin(double fahrenheit) {
double kelvin = (5.0 / 9.0) * (fahrenheit - 32) + 273.15;
return kelvin;
}
int main() {
double fahrenheit;
cout << "Enter the temperature in Fahrenheit: ";
cin >> fahrenheit;
double kelvin = fahrenheitToKelvin(fahrenheit);
cout << "Temperature in Kelvin: " << kelvin << endl;
return 0;
}
- Include Libraries:
- The
#include <iostream>
statement allows input and output operations in C++. The
using namespace std;
line allows the usage of elements in thestd
namespace without explicitly using the prefixstd::
.Function Definition:
- The
fahrenheitToKelvin
function takes a Fahrenheit temperature as input and returns the equivalent temperature in Kelvin. Inside the function, the conversion formula from Fahrenheit to Kelvin is applied.
Main Function:
- The
main
function is where the program execution begins. - It prompts the user to enter a temperature in Fahrenheit and reads the input.
- It then calls the
fahrenheitToKelvin
function to convert the entered Fahrenheit temperature to Kelvin. Finally, it displays the temperature in Kelvin.
Conversion Formula:
- The conversion formula
(5.0 / 9.0) * (fahrenheit - 32) + 273.15
transforms the Fahrenheit temperature to Kelvin.