Objective
Write a C# program that asks for a symbol and a width, and displays a hollow square of that width using that symbol for the outer border, as shown in this example:
Enter a symbol: 4
Enter the desired width: 3
444
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 for the square
Console.Write("Enter a symbol: ");
char symbol = Console.ReadKey().KeyChar; // Read a single character input from the user
Console.WriteLine(); // Move to the next line after reading the symbol
// Prompt the user to enter the width of the square
Console.Write("Enter the desired width: ");
int width = int.Parse(Console.ReadLine()); // Read and convert the width to an integer
// Loop through the rows of the square
for (int i = 1; i <= width; i++) // Loop for each row
{
// Check if it's the first or last row
if (i == 1 || i == width) // If it's the first or last row
{
for (int j = 1; j <= width; j++) // Loop through each column
{
Console.Write(symbol); // Print the symbol for the border
}
}
else // For the middle rows
{
Console.Write(symbol); // Print the symbol for the left border
for (int j = 2; j < width; j++) // Loop through the middle columns
{
Console.Write(" "); // Print a space for the hollow part
}
Console.Write(symbol); // Print the symbol for the right border
}
Console.WriteLine(); // Move to the next line after finishing the row
}
}
}