Conversion between degree Celsius and Fahrenheit

In this tutorial, we will address two questions from our list of basic C++ exercises, about converting between Celsius and Fahrenheit temperatures.

The formulas that we will use are:

How to convert from Fahrenheit to Celsius in C ++

How to convert from Celsius to Fahrenheit

Initially, let's do from C to F.
That is, the user enters the value in Celsius and the program returns the Fahrenheit.

The mathematical formula is the first above, which in C ++ language turns:
F = (9 * C / 5) + 32;

Look:
#include <iostream>
using namespace std;

int main()
{
    float C, F;

    cout << "Celsius degrees: ";
    cin >> C;

    F = (9*C/5) + 32;

    cout << "Represents in Fahrenheit: " << F;

    return 0;
}

Converting from Fahrenheit to Celsius

Now the opposite, using the second formula, which in C ++ is:
C = 5 * (F-32) / 9;

See how our code looks:

#include <iostream>
using namespace std;

int main()
{
    float C, F;

    cout << "Fahrenheit degrees: ";
    cin >> F;

    C = 5*(F-32)/9;

    cout << "Represents in Celsius: " << C;

    return 0;
}

No comments:

Post a Comment