Ejercicio
Cuadrado hueco
Objetivo
Escribe un programa en C# que pida un símbolo, un ancho y muestre un cuadrado hueco de ese ancho, usando ese número para el símbolo exterior, como en este ejemplo:
Introduzca un símbolo: 4
Introduzca el ancho deseado: 3
444
4 4
444
Código de Ejemplo
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
}
}
}