Exercise
Hollow Rectangle
Objective
Write a C# program that prompts for a symbol, a width, and a height, and displays a hollow rectangle of that width and height, using that symbol for the outer border, as in this example:
Enter a symbol: 4
Enter the desired width: 3
Enter the desired height: 5
444
4 4
4 4
4 4
444
Write Your C# Exercise
C# Exercise Example
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
// Prompt the user to enter a symbol
Console.Write("Enter a symbol: ");
char symbol = Console.ReadKey().KeyChar; // Read a symbol from the user
Console.WriteLine(); // Move to the next line after symbol input
// Prompt the user to enter the desired width
Console.Write("Enter the desired width: ");
int width = int.Parse(Console.ReadLine()); // Read the width and convert it to integer
// Prompt the user to enter the desired height
Console.Write("Enter the desired height: ");
int height = int.Parse(Console.ReadLine()); // Read the height and convert it to integer
// Loop through each row
for (int i = 0; i < height; i++)
{
// Loop through each column in the current row
for (int j = 0; j < width; j++)
{
// Check if it's the border (first or last row, or first or last column)
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
{
Console.Write(symbol); // Print the symbol for the border
}
else
{
Console.Write(" "); // Print space for the inner part of the rectangle
}
}
Console.WriteLine(); // Move to the next line after each row
}
}
}