Examples C++

Temperature Conversions with Fahrenheit and Celsius

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.

Code:

#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:

Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.