404 Not Found

404 Not Found


nginx

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:

Conversion formulas:

Example

CSHARP
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.");
}
▶ Try it Yourself
TEXT
Temperature Converter
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
Choose (1 or 2): 1
Enter Celsius temperature: 100
100°C = 212.00°F
TEXT
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:

Example

CSHARP
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;
}
▶ Try it Yourself
TEXT
Enter the first number: 10
Enter an operator (+ - * /): +
Enter the second number: 3.5
10 + 3.5 = 13.5
TEXT
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:

Example

CSHARP
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);
▶ Try it Yourself
TEXT
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:

Example

CSHARP
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();
}
▶ Try it Yourself
TEXT
Enter the number of rows: 5
Right Triangle:
*
**
***
****
*
Pyramid:
    *
   ***
  *
 ***
*****

❓ FAQ

Q What happens if double.Parse receives non-numeric input?
A It throws a FormatException. In real projects, use double.TryParse for safe conversion.
Q Why is the upper bound of Random.Next(1, 101) 101?
A The method uses a half-open interval - Next(1, 101) generates integers from 1 to 100, excluding 101.
Q How is the number of stars per pyramid row calculated?
A Row i has 2 * i - 1 stars - row 1 has 1, row 2 has 3, row 3 has 5, and so on.
Q What is the difference between do...while and while?
A 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

📝 Exercises

  1. Add a loop to the temperature converter so the user can convert repeatedly until entering q to quit.
  2. Add modulo (%) operation support to the simple calculator.
  3. Improve the number guessing game: when the user enters a non-numeric value, prompt them to try again instead of crashing.
  4. Print an inverted pyramid pattern (widest row at the top, narrowest at the bottom).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏