Description:

In this C++ example, we demonstrate how to convert temperatures between the Fahrenheit and Celsius scales. To do the temperature conversions, we provide two functions: one converts from Fahrenheit to Celsius and the other converts from Celsius to Fahrenheit. We call each of the functions once to demonstrate the temperature conversions.

Example

#include <iostream>

double CelsiusToFahrenheit(double dC) {
    return (dC*9.0/5.0) + 32.0;
}

double FahrenheitToCelsius(double dF) {
    return (dF - 32.0)*5.0/9.0;
}

int main() {
    using namespace std;

    // Perform a conversion in each direction
    double dC = 20;
    cout << dC << " degrees Celsius converts to " << CelsiusToFahrenheit(dC)
        << " degrees Fahrenheit." << endl;

    double dF = 77;
    cout << dF << " degrees Fahrenheit converts to " << FahrenheitToCelsius(dF)
        << " degrees Celsius." << endl;

    return 0;
}

Output:

Temperature Output