Rectangle V3 - C# Programming Exercise

In this C# exercise, you are asked to create a program that asks the user for their name and a size, then displays a hollow rectangle made from that name.

The program should work as follows:

- First, ask the user for their name.
- Then, ask for the size of the rectangle, which defines both the width and height.
- The rectangle should be formed by repeating the name in each row, but the middle rows should only have the name at the edges, leaving spaces inside the rectangle.

This exercise will help you practice using loops and string manipulation in C#, as well as teaching you how to control the display of blank spaces in the console.

 Category

Arrays, Structures and Strings

 Exercise

Rectangle V3

 Objective

Write a C# program to ask the user for his/her name and a size, and display a hollow rectangle with it:

Enter your name: Yo
Enter size: 4
YoYoYoYo
Yo____Yo
Yo____Yo
YoYoYoYo

(note: the underscores _ should not be displayed on screen; you program should display blank spaces inside the rectangle)

 Write Your C# Exercise

using System;  // Import the System namespace for basic functionality

class Program  // Define the main class
{
    static void Main()  // The entry point of the program
    {
        // Ask the user for their name and size of the rectangle
        Console.Write("Enter your name: ");
        string name = Console.ReadLine();  // Read the user's input as a string

        Console.Write("Enter size: ");
        int size = int.Parse(Console.ReadLine());  // Read the size input as an integer

        // Print the top and bottom edges of the rectangle
        string topBottom = string.Concat(Enumerable.Repeat(name, size));
        Console.WriteLine(topBottom);  // Print the top edge

        // Print the middle rows of the rectangle
        for (int i = 1; i < size - 1; i++)
        {
            // Print the left part, followed by spaces, and then the right part
            Console.Write(name);  // Print the first part of the name
            Console.Write(new string(' ', (size - 2) * name.Length));  // Print spaces
            Console.WriteLine(name);  // Print the last part of the name
        }

        // Print the bottom edge of the rectangle (same as the top)
        if (size > 1)  // To prevent repeating the top if the size is 1
        {
            Console.WriteLine(topBottom);  // Print the bottom edge
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

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#.

  •  Centered triangle

    In this C# exercise, you are asked to create a program that asks the user for their name and a size, then displays a hollow rectangle made from t...

  •  Cities database

    In this exercise, you are asked to create a database to store information about cities. In the first approach, you will store only the name...

  •  Banner

    This Exercise in C# involves creating a program that imitates the basic Unix SysV "banner" utility, allowing you to display large texts in a similar manner. The purpo...

  •  Triangle right side

    This Exercise in C# involves creating a program that asks the user for a text string and displays a right-aligned triangle using that string. The triang...

  •  Strings manipulation

    This Exercise in C# involves creating a program that asks the user for a text string and performs three specific transformations on it. The program must...

  •  Nested structs

    This Exercise in C# involves creating a struct to store two pieces of data for a person: their name and their date of birth. The date of birth...