Practice: Basics Review
Temperature Converter
Goal: Implement bidirectional conversion between Celsius and Fahrenheit. The user selects the conversion direction and enters a temperature, and the program outputs the result.
Requirements:
- Prompt the user to choose a direction (1: Celsius to Fahrenheit, 2: Fahrenheit to Celsius)
- Read the temperature value from the user
- Apply the conversion formula and display the result (two decimal places)
- Handle invalid selections with a helpful message
Conversion formulas:
- Celsius to Fahrenheit:
F = C * 9 / 5 + 32 - Fahrenheit to Celsius:
C = (F - 32) * 5 / 9
Example
Console.WriteLine("Temperature Converter");
Console.WriteLine("1. Celsius to Fahrenheit");
Console.WriteLine("2. Fahrenheit to Celsius");
Console.Write("Choose (1 or 2): ");
string choice = Console.ReadLine();
if (choice == "1")
{
Console.Write("Enter Celsius temperature: ");
double celsius = double.Parse(Console.ReadLine());
double fahrenheit = celsius * 9 / 5 + 32;
Console.WriteLine($"{celsius}°C = {fahrenheit:F2}°F");
}
else if (choice == "2")
{
Console.Write("Enter Fahrenheit temperature: ");
double fahrenheit = double.Parse(Console.ReadLine());
double celsius = (fahrenheit - 32) * 5 / 9;
Console.WriteLine($"{fahrenheit}°F = {celsius:F2}°C");
}
else
{
Console.WriteLine("Invalid choice. Please enter 1 or 2.");
}
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Choose (1 or 2): 1
Enter Celsius temperature: 100
100°C = 212.00°F
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Choose (1 or 2): 2
Enter Fahrenheit temperature: 32
32°F = 0.00°C
Simple Calculator
Goal: Build a calculator that supports the four basic arithmetic operations. The user enters two numbers and an operator, and the program displays the result while handling division by zero.
Requirements:
- Prompt the user for two numbers and an operator (+, -, *, /)
- Use a
switchstatement to match the operator and perform the corresponding operation - Check for division by zero and display a friendly error message
- Display a message for invalid operators
Example
Console.Write("Enter the first number: ");
double num1 = double.Parse(Console.ReadLine());
Console.Write("Enter an operator (+ - * /): ");
string op = Console.ReadLine();
Console.Write("Enter the second number: ");
double num2 = double.Parse(Console.ReadLine());
double result;
switch (op)
{
case "+":
result = num1 + num2;
Console.WriteLine($"{num1} + {num2} = {result}");
break;
case "-":
result = num1 - num2;
Console.WriteLine($"{num1} - {num2} = {result}");
break;
case "*":
result = num1 * num2;
Console.WriteLine($"{num1} * {num2} = {result}");
break;
case "/":
if (num2 == 0)
{
Console.WriteLine("Error: Division by zero is not allowed!");
}
else
{
result = num1 / num2;
Console.WriteLine($"{num1} / {num2} = {result}");
}
break;
default:
Console.WriteLine("Invalid operator!");
break;
}
Enter the first number: 10
Enter an operator (+ - * /): +
Enter the second number: 3.5
10 + 3.5 = 13.5
Enter the first number: 8
Enter an operator (+ - * /): /
Enter the second number: 0
Error: Division by zero is not allowed!
Number Guessing Game
Goal: The program generates a random integer from 1 to 100. The user repeatedly guesses, and the program hints "too high" or "too low" until the correct number is found, then displays the total attempts.
Requirements:
- Use the
Randomclass to generate a random integer from 1 to 100 - Use a
whileloop to let the user guess repeatedly - After each guess, give a "too high" or "too low" hint
- When guessed correctly, display a congratulations message and the total number of attempts
Example
Random random = new Random();
int target = random.Next(1, 101);
int guess;
int attempts = 0;
Console.WriteLine("Number Guessing Game (1-100)");
do
{
Console.Write("Enter your guess: ");
guess = int.Parse(Console.ReadLine());
attempts++;
if (guess > target)
{
Console.WriteLine("Too high!");
}
else if (guess < target)
{
Console.WriteLine("Too low!");
}
else
{
Console.WriteLine($"Congratulations! The answer is {target}. You got it in {attempts} attempts.");
}
} while (guess != target);
Number Guessing Game (1-100)
Enter your guess: 50
Too low!
Enter your guess: 75
Too high!
Enter your guess: 62
Too low!
Enter your guess: 68
Congratulations! The answer is 68. You got it in 4 attempts.
Star Patterns
Goal: Use nested for loops to print two common star patterns - a right triangle and a pyramid - to practice loops and string concatenation.
Requirements:
- Right triangle: print i stars on row i
- Pyramid: print the appropriate number of leading spaces and stars on each row to center the stars
- The number of rows is determined by user input
Example
Console.Write("Enter the number of rows: ");
int rows = int.Parse(Console.ReadLine());
Console.WriteLine("Right Triangle:");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Console.WriteLine("Pyramid:");
for (int i = 1; i <= rows; i++)
{
for (int s = 1; s <= rows - i; s++)
{
Console.Write(" ");
}
for (int j = 1; j <= 2 * i - 1; j++)
{
Console.Write("*");
}
Console.WriteLine();
}
Enter the number of rows: 5
Right Triangle:
*
**
***
****
*
Pyramid:
*
***
*
***
*****
❓ FAQ
double.Parse receives non-numeric input?FormatException. In real projects, use double.TryParse for safe conversion.Random.Next(1, 101) 101?Next(1, 101) generates integers from 1 to 100, excluding 101.2 * i - 1 stars - row 1 has 1, row 2 has 3, row 3 has 5, and so on.do...while and while?do...while executes the body at least once; while may not execute it at all. The guessing game requires at least one guess, so do...while is a better fit.📖 Summary
- The temperature converter practiced
if...else ifconditionals and math operations - The simple calculator practiced
switchbranching and division-by-zero handling - The number guessing game practiced
do...whileloops and theRandomclass - The star patterns practiced nested
forloops and string concatenation techniques
📝 Exercises
- Add a loop to the temperature converter so the user can convert repeatedly until entering
qto quit. - Add modulo (
%) operation support to the simple calculator. - Improve the number guessing game: when the user enters a non-numeric value, prompt them to try again instead of crashing.
- Print an inverted pyramid pattern (widest row at the top, narrowest at the bottom).



