C++ Code example to convert Fahrenheit temperature to Celsius

C++ Code example to convert Fahrenheit temperature to Celsius

June 28, 2024·İbrahim Korucuoğlu
İbrahim Korucuoğlu

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;
}
Last updated on