You’ve learned how to store values in variables, perform arithmetic, and convert user input into numbers. In this exercise, you put it all together by writing a program that converts a Fahrenheit temperature to Celsius.
This exercise takes approximately 15 minutes.
You’ll write and run your code using an online C# editor — no installation required.
Open a browser and navigate to the C# editor at https://microsoftlearning.github.io/c-sharp-minor.
Before writing any logic, you’ll paste a set of guiding comments into the editor. These comments act as an outline for your program — each one marks where a specific piece of code belongs.
Copy the following comments and paste them into the editor pane:
// Store a Fahrenheit temperature
// Convert it to Celsius
// Display the result
Remember, comments are ignored by C# when the program runs. They’re just there to help you organize your code.
Beneath the // Store a Fahrenheit temperature comment, declare a variable that stores a Fahrenheit temperature, such as 94.
Tip: Think about which data type to use. The conversion formula in the next section involves division, and you’ll want to keep the decimal part of the result. Look back at how you declared money values in the Build a tip calculator exercise for a type that works well here.
Beneath the // Convert it to Celsius comment, add code that converts the Fahrenheit value to Celsius using the formula:
Store the result in a new variable.
Tip: If you use
intfor the formula, C# performs integer division, which drops any decimal places and gives you the wrong answer. Usingdecimalfor every value in the formula keeps the fractional part.
Beneath the // Display the result comment, add a Console.WriteLine() statement that uses string interpolation to display both the original Fahrenheit value and the converted Celsius value.
Select the ▶️ Run button to run your program. For a Fahrenheit value of 94, your output should look similar to:
94°F is 34.44444444444444444444444444°C
Note: That’s a lot of decimal places! You’ll clean this up in the Code with AI section below.
Select the Clear console button before you continue.
Compare your program to the example below. Your code doesn’t need to match exactly — there are many ways to write a program that works correctly — but the behavior should be the same.
// Store a Fahrenheit temperature
decimal fahrenheit = 94;
// Convert it to Celsius
decimal celsius = (fahrenheit - 32) * 5 / 9;
// Display the result
Console.WriteLine($"{fahrenheit}°F is {celsius}°C");
Using an AI assistant (like Copilot) is a great way to explore a programming language at your own pace. Try each prompt below, read the response carefully, and test the code examples it gives you in the online editor.
AI Prompt:
“In C#, how can I display a decimal value rounded to one decimal place, like 34.4 instead of 34.44444444444444444444444444? Can you show me beginner friendly examples?”
After the AI responds: Try using the technique it suggests to clean up your Celsius output so it only shows one decimal place.
AI Prompt:
“In C#, how can I ask the user to type a temperature and convert their answer into a decimal I can use in a calculation? Can you show me beginner friendly examples?”
After the AI responds: Try updating your program so the user enters the Fahrenheit temperature instead of it being hardcoded.
Once your basic version works, try supporting the other direction: convert a Celsius temperature to Fahrenheit using the formula F = (C × 9 / 5) + 32.