C++ Code example to convert Fahrenheit temperature to Celsius

Categories:
less than a minute
In this example, the entered Fahrenheit temperature value is converted to Celsius value with the help of a function.
#include <iostream>
#include <iomanip>
#include <limits>
float temperatureConversion(const float temperatureInFahrenheit) {
constexpr float conversionFactor = 5.0 / 9.0;
return (temperatureInFahrenheit - 32) * conversionFactor;
}
int main() {
float fahrenheitTemperature;
std::cout << "Enter the Fahrenheit temperature: ";
std::cin >> fahrenheitTemperature;
float celsiusTemperature = temperatureConversion(fahrenheitTemperature);
std::cout << std::fixed << std::setprecision(std::numeric_limits<float>::digits10) << "Celsius value: " <<
celsiusTemperature << std::endl;
return 0;
}
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.