Blog
Wild & Free Tools

Celsius to Fahrenheit in Python and JavaScript — Code Examples

Last updated: March 2026 5 min read
Quick Answer

Table of Contents

  1. The Formula Explained
  2. Python — Celsius to Fahrenheit
  3. JavaScript — Celsius to Fahrenheit
  4. Java — Celsius to Fahrenheit
  5. Verifying Your Output
  6. Frequently Asked Questions

The formula for Celsius to Fahrenheit is F = (C × 9/5) + 32. In code, this is a single arithmetic expression. Whether you are completing a FreeCodeCamp challenge, a homework problem, or building a weather app, the implementation is the same core formula in any language.

Use the converter above to verify your code outputs against the correct answer before submitting.

The Formula Explained

°F = (°C × 9/5) + 32

Two operations: (1) multiply Celsius by 9/5 (or equivalently 1.8) to scale the degree size, (2) add 32 to shift the zero point. The scaling step accounts for the fact that 100 Celsius degrees span the same range as 180 Fahrenheit degrees (100 × 9/5 = 180). The +32 shift accounts for Fahrenheit starting at 32 instead of 0 at the freezing point of water.

The reverse (F to C): °C = (°F − 32) × 5/9. Same operations in reverse order.

Python — Celsius to Fahrenheit

def celsius_to_fahrenheit(c):
    return (c * 9/5) + 32

# Examples
print(celsius_to_fahrenheit(0))    # 32.0
print(celsius_to_fahrenheit(100))  # 212.0
print(celsius_to_fahrenheit(37))   # 98.6
print(celsius_to_fahrenheit(-40))  # -40.0

For user input: accept a float or int, apply the formula, round if needed. Common FreeCodeCamp pattern asks for a function that takes celsius and returns fahrenheit — the function above is the solution.

Sell Custom Apparel — We Handle Printing & Free Shipping

JavaScript — Celsius to Fahrenheit

function celsiusToFahrenheit(c) {
  return (c * 9/5) + 32;
}

// Arrow function version
const celsiusToFahrenheit = c => (c * 9/5) + 32;

// Examples
console.log(celsiusToFahrenheit(0));    // 32
console.log(celsiusToFahrenheit(100));  // 212
console.log(celsiusToFahrenheit(37));   // 98.6

For rounding: wrap the result in Math.round() or use toFixed(1) for one decimal place: (celsiusToFahrenheit(37)).toFixed(1) returns "98.6".

Java — Celsius to Fahrenheit

public class TempConverter {
    public static double celsiusToFahrenheit(double c) {
        return (c * 9.0/5.0) + 32;
    }

    public static void main(String[] args) {
        System.out.println(celsiusToFahrenheit(100)); // 212.0
        System.out.println(celsiusToFahrenheit(37));  // 98.6
    }
}

Note: use 9.0/5.0 instead of 9/5 in Java to force floating-point division. In Java, 9/5 with integer literals performs integer division and returns 1 instead of 1.8, giving a wrong answer.

Verifying Your Output

Use the unit converter above to check any Celsius input against the expected Fahrenheit output. Common test cases:

If your code passes all four of these edge cases, it is almost certainly correct.

Check Your Code Output Instantly

Enter any Celsius value and verify the Fahrenheit result — free, no signup, browser-based.

Open Unit Converter Free

Frequently Asked Questions

What is the formula for Celsius to Fahrenheit?

F = (C × 9/5) + 32. Equivalently: F = (C × 1.8) + 32.

How do I write a Celsius to Fahrenheit function in Python?

def celsius_to_fahrenheit(c): return (c * 9/5) + 32. This single line is the complete implementation.

Why does Java give the wrong answer for 9/5?

In Java, dividing two integer literals performs integer division: 9/5 = 1, not 1.8. Write 9.0/5.0 or cast to double to get the correct result.

What is the FreeCodeCamp Celsius to Fahrenheit challenge?

The challenge asks you to write a function that takes a Celsius value and returns the Fahrenheit equivalent using the formula F = C * 9/5 + 32. The solution is one line in Python or JavaScript.

Carlos Mendez
Carlos Mendez Photo Editing & Image Writer

Carlos has been a freelance photographer and photo editor for a decade, working with clients from local businesses to regional magazines.

More articles by Carlos →
Launch Your Own Clothing Brand — No Inventory, No Risk