Programming Course in C# ¡Free!

Hollow square

 Friday, April 05, 2013 published by Exercises C#
Proposed exercise

Write a program which asks for a symbol, a width and displays a hollow square of that width, using that number for the outer symbol, as in this example:


Enter a symbol: 4

Enter the desired width: 3


444
4 4
444


Output



Solution


using System;
public class HollowSquare
{
public static void Main()
{    
Console.Write("Enter number the border: ");
int border=Convert.ToInt32(Console.ReadLine());

Console.Write("Enter width: ");
int width = Convert.ToInt32(Console.ReadLine());

int i;
for( i=0; i < width; i++ )
Console.Write(border);

for( i=0;i < width - 2;i++ )
{
Console.Write( border );

for(j=0; j < width - 2;j++)
Console.Write(" ");

Console.WriteLine( border );
}   

for(i=0;i < width;i++)
Console.Write(border);

Console.WriteLine();
}
}