Lesson 26 +25 XP

Project 2: Temperature Converter

Project 2: Temperature Converter

Convert Celsius to Fahrenheit (and back) with a clean menu.

The goal

Ask the user which direction to convert and a temperature, then print the rounded result.

What you practice

  • if / else if / else branches
  • Convert.ToInt32 and Convert.ToDouble
  • Math: (c 9) / 5 + 32 and (f - 32) 5 / 9
  • Math.Round

Starter code

using System;

Console.WriteLine("1. Celsius to Fahrenheit");
Console.WriteLine("2. Fahrenheit to Celsius");
int choice = Convert.ToInt32(Console.ReadLine());

if (choice == 1)
{
  Console.WriteLine("Celsius:");
  double c = Convert.ToDouble(Console.ReadLine());
  double f = (c * 9) / 5 + 32;
  Console.WriteLine(Math.Round(f, 1) + " F");
}
else if (choice == 2)
{
  Console.WriteLine("Fahrenheit:");
  double f = Convert.ToDouble(Console.ReadLine());
  double c = (f - 32) * 5 / 9;
  Console.WriteLine(Math.Round(c, 1) + " C");
}
else
{
  Console.WriteLine("Unknown choice");
}

Step-by-step

  1. Run the starter and test both conversions with 0 and 32.
  2. Add a guard that rejects choices that aren't 1 or 2.
  3. Wrap the conversions in a try/catch.
  4. Re-run with nonsense input to confirm it doesn't crash.

Checklist

  • [ ] Both conversion formulas are used
  • [ ] The choice branch works
  • [ ] Results print with one decimal
  • [ ] Bad input is handled
  • [ ] The program runs without errors

TL;DR

  • Branch with if/else if/else.
  • Convert strings with Convert.ToDouble.
  • Math.Round(x, 1) keeps one decimal.