Create programs to convert Celsius to Fahrenheit and back
Here are two simple example programs (one in Python, one in JavaScript) that convert Celsius to Fahrenheit using F = (C × 9/5) + 32, and convert Fahrenheit back to Celsius using the inverse formula C = (F − 32) × 5/9.
Python (command-line):
def c_to_f(c):
return (c * 9/5) + 32
def f_to_c(f):
return (f - 32) * 5/9
def main():
try:
c = float(input("Enter temperature in Celsius: "))
print(f"{c} °C = {c_to_f(c):.2f} °F")
f = float(input("Enter temperature in Fahrenheit: "))
print(f"{f} °F = {f_to_c(f):.2f} °C")
except ValueError:
print("Please enter a valid number.")
if __name__ == "__main__":
main()
JavaScript (browser console or webpage):
function cToF(c) {
return (c * 9/5) + 32;
}
function fToC(f) {
return (f - 32) * 5/9;
}
// Example usage:
console.log("25 °C =", cToF(25).toFixed(2), "°F");
console.log("77 °F =", fToC(77).toFixed(2), "°C");
If you want a small HTML page with input fields and live conversion, say so and I’ll provide it.
Was this answer helpful?
Thanks — your feedback improves the quality gate.