Hollow Rectangle - C# Programming Exercise

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 specified dimensions, using the symbol for the outer border. The hollow rectangle has its border made with the symbol and the inner space left empty. This kind of structure is useful for learning how to work with nested loops, which allow controlling both the rows and columns of the rectangle. Additionally, conditional structures are used to identify when to draw the border and when to leave an empty space inside the rectangle. In this exercise, the user will have the chance to practice input and apply the logic needed to create patterns on the console. It is an excellent exercise for learning how to handle control structures like if, as well as using for or while loops.

 Category

Flow Control

 Exercise

Hollow Rectangle

 Objective

Write a C# program that prompts for a symbol, a width, and a height, and displays a hollow rectangle of that width and height, using that symbol for the outer border, as in this example:

Enter a symbol: 4
Enter the desired width: 3
Enter the desired height: 5

444
4 4
4 4
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
        Console.Write("Enter a symbol: ");
        char symbol = Console.ReadKey().KeyChar; // Read a symbol from the user
        Console.WriteLine(); // Move to the next line after symbol input

        // Prompt the user to enter the desired width
        Console.Write("Enter the desired width: ");
        int width = int.Parse(Console.ReadLine()); // Read the width and convert it to integer

        // Prompt the user to enter the desired height
        Console.Write("Enter the desired height: ");
        int height = int.Parse(Console.ReadLine()); // Read the height and convert it to integer

        // Loop through each row
        for (int i = 0; i < height; i++)
        {
            // Loop through each column in the current row
            for (int j = 0; j < width; j++)
            {
                // Check if it's the border (first or last row, or first or last column)
                if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
                {
                    Console.Write(symbol); // Print the symbol for the border
                }
                else
                {
                    Console.Write(" "); // Print space for the inner part of the rectangle
                }
            }
            Console.WriteLine(); // Move to the next line after each 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#.

  •  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: ...

  •  Prime number

    This C# exercise aims to develop a program that asks the user for an integer and determines if it is a prime number or not. A prime number is one...

  •  Give change

    This exercise in C# aims to develop a program that calculates the change for a purchase, using the largest possible coins or bills. The program s...

  •  Exceptions

    This exercise in C# aims to develop a program that asks the user for two numbers and displays their division. The program should handle potential errors...