2.25 - Hollow square

Objetive

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


Solution

using System;
public class HollowSquare
{
    public static void Main()
    {
        int n;
        int i,j;
            
        Console.Write("Enter number for the border of the square: ");
        n=Convert.ToInt32(Console.ReadLine());
        
        Console.Write("Enter total: ");
        width=Convert.ToInt32(Console.ReadLine());
        
        // First row
        
        for(i=0;i;i++)
            Console.Write(n);
        
        Console.WriteLine();
        
        // Middle x-2 rows
        
        for(i=0;i-2;i++)
        {
            Console.Write(n);
            
            for(j=0;j-2;j++)
                Console.Write(" ");
                
            Console.WriteLine(n);
        }   
        
        // Last row
        
        for(i=0;i;i++)
            Console.Write(n);
        
        Console.WriteLine();
    }
}
     

Popular Posts