C++ bootcamp · Lab 03

Celsius to Fahrenheit

easyMath8 minLesson: C++ Math

Read the question, write C++ on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read a Celsius temperature (it may have decimals) and print it in Fahrenheit.

F = C × 9/5 + 32. Print the result with exactly one digit after the decimal point.

Input. One line: a number.

Output. Fahrenheit to one decimal, e.g. 98.6

Constraints

  • Read into a double, not an int.

Examples

Example 1 — Body temperature.
Input
37
Output
98.6
Example 2 — std::fixed keeps the trailing .0.
Input
100
Output
212.0
Hint
  1. Read a double so decimals survive: double c; std::cin >> c;
  2. #include <iomanip>, then std::cout << std::fixed << std::setprecision(1) << value;
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <iostream>
#include <iomanip>

int main() {
  double c;
  std::cin >> c;
  std::cout << std::fixed << std::setprecision(1) << (c * 9 / 5 + 32) << "\n";
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.