C++20 sections · 1024 units
Open in Course

Temperature Convert - Implementation

Working solution

I declare celsius as a double to handle decimal input. Then I apply the conversion formula, making sure to use 9.0 and 5.0 to get floating-point division. Finally, I set precision to 2 decimal places and output the result.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
 double celsius;
 cin >> celsius;
 double fahrenheit = celsius * 9.0 / 5.0 + 32;
 cout << fixed << setprecision(2);
 cout << fahrenheit << endl;
 return 0;
}
``` You could write the formula in one line without storing fahrenheit, but using a variable makes the code clearer. Notice how I included iomanip for the formatting functions.