Write Python. Understand every error.

Open the editor

Python programs · Numbers

Python program to convert Celsius to Fahrenheit

Converting temperatures is a short program that practises decimals and formatting.

Approach

  1. Read the temperature in Celsius as a decimal number.
  2. Multiply by 9, divide by 5 and add 32.
  3. Print the result with one decimal place.

The program

c = float(input())
f = c * 9 / 5 + 32
print(f"{c} C = {f:.1f} F")

Sample run

Input
37
Output
37.0 C = 98.6 F

Run this program in Py-CRAFT

How the code works

The formula is F = C × 9 ÷ 5 + 32. Body temperature, 37 °C, becomes 98.6 °F. {f:.1f} inside the f-string rounds to one decimal place. float() is used instead of int() so that readings such as 36.6 are accepted.

Try changing it

Common mistakes

Learn the ideas

More programs

Share this page: