cppbeginner10 minutes

Fix the Bug in Celsius to Fahrenheit Conversion Function

A simple function to convert a temperature from Celsius to Fahrenheit contains a bug. Fix the function so it returns the correct Fahrenheit value.

Challenge prompt

The function celsiusToFahrenheit is intended to convert a temperature from Celsius to Fahrenheit using the formula F = C * 9/5 + 32. However, it currently returns the wrong result. Identify and fix the bug so that the function correctly converts Celsius to Fahrenheit.

Guidance

  • Review the formula for Celsius to Fahrenheit conversion.
  • Check the order of operations and make sure to use floating point division if needed.

Hints

  • Integer division may be truncating your calculation result.
  • Try using 9.0/5.0 instead of 9/5.

Starter code

double celsiusToFahrenheit(int celsius) {
    return celsius * 9 / 5 + 32;
}

Expected output

celsiusToFahrenheit(0) => 32 celsiusToFahrenheit(100) => 212 celsiusToFahrenheit(-40) => -40

Core concepts

basic arithmeticorder of operationstype conversion

Challenge a Friend

Send this duel to someone else and see if they can solve it.