Hollow Square - C# Programming Exercise

This C# exercise aims to develop a program that asks the user for a symbol and a width, and then displays a hollow square of that width using the provided symbol for the outer border. The program utilizes control flow structures such as conditional statements and loops to manage the arrangement of the symbol in the square, ensuring that the outer border is made of the chosen symbol while the interior remains empty. This type of exercise is useful for understanding how to work with nested loops, like for loops, to create patterns or geometric figures in the console. It is also important for learning how to handle user input and use that input to create simple graphical representations in the console. Additionally, this exercise reinforces the use of conditions and how to check whether a position corresponds to the border or the interior of the square. It is a fundamental exercise for beginner programmers looking to improve their skills in string manipulation and conditional logic in programming languages like C#.

 Category

Flow Control

 Exercise

Hollow Square

 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

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
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Product

    This C# exercise aims to develop a program that asks the user for two integer numbers and shows their multiplication, but without using the "*" operator. Inste...

  •  Absolute value

    This C# exercise aims to develop a program that calculates (and displays) the absolute value of a number x. The absolute value of a number is its distan...

  •  Hollow rectangle

    This C# exercise aims to develop a program that prompts the user for a symbol, a width, and a height, then displays a hollow rectangle of the spe...

  •  Statistics

    This C# exercise aims to develop a program that allows the user to input several numbers and calculate basic statistical operations such as sum, average, mi...

  •  Switch

    This C# exercise aims to develop a program that, given a numerical grade, displays the corresponding text grade according to the following equivalence: 9 and ...

  •  Conditional operator, positive & smaller

    This C# exercise aims to develop a program that asks the user for two numbers and uses the conditional operator (?) to answer the following questions: ...