C++ Code Example: convert celsius to fahrenheit

This code converts a temperature from Celsius to Fahrenheit.

Code Example

#include <iostream>
using namespace std;

int main() {
    double c, f;

    cout << "Please enter temperature in celsius: ";
    cin >> c;

    f = c * 1.8 + 32;

    cout << "Temperature in fahrenheit: " << f << endl;

    return 0;
}
Output
Please enter temperature in celsius: 10
Temperature in fahrenheit: 50

Code Explanation

The main function starts by declaring two double variables, c and f, to represent the temperatures in Celsius and Fahrenheit, respectively.

Next, the program prompts the user to input the temperature in Celsius using cout and stores the value in the c variable using cin.

The temperature conversion formula from Celsius to Fahrenheit is then applied: f = c * 1.8 + 32. This formula multiplies the Celsius temperature by 1.8 and then adds 32 to it.

Finally, the converted temperature in Fahrenheit is printed to the console using cout. The program ends by returning a value of 0 to indicate successful execution.