Convert Celsius to Fahrenheit
Description: Convert 0, 37 and 100 degrees Celsius to Fahrenheit with F = C * 9 / 5 + 32, and print one digit after the decimal point.
Hint:
- Use
doubleand9.0 / 5.
Solution
#include <print>
double to_fahrenheit(double c) {
return c * 9.0 / 5 + 32;
}
int main() {
for (double c : {0.0, 37.0, 100.0}) {
std::println("{:.1f} C = {:.1f} F", c, to_fahrenheit(c));
}
}
Output
0.0 C = 32.0 F
37.0 C = 98.6 F
100.0 C = 212.0 F
How it works
9.0 / 5is a decimal division.9 / 5would be 1 (integer division).{:.1f}prints one digit after the point.
Read C++ Functions.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |