Rectangle V2 - C# Programming Exercise

This C# exercise aims to develop a program that asks the user for a number, a width, and a height, and then displays a rectangle with the provided number as the inner symbol, using the given width and height values. The program should repeat the number as many times as the width of the rectangle, and do so for as many rows as the height of the rectangle. This exercise is helpful for learning how to work with nested loops, which are used to control the rows and columns of the rectangle. Additionally, the use of user input allows practicing how to capture and process data entered by the user in C#, which is essential for creating interactive applications.

 Category

Flow Control

 Exercise

Rectangle V2

 Objective

Write a C# program that asks for a number, width, and height, and displays a rectangle of that width and height, using that number for the inner symbol, as shown in the example below:

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

444
444
444
444
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 the number to display inside the rectangle
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine());  // Reading the number entered by the user

        // Prompt the user to enter the desired width of the rectangle
        Console.Write("Enter the desired width: ");
        int width = int.Parse(Console.ReadLine());  // Reading the width entered by the user

        // Prompt the user to enter the desired height of the rectangle
        Console.Write("Enter the desired height: ");
        int height = int.Parse(Console.ReadLine());  // Reading the height entered by the user

        // Loop to print the rectangle row by row
        for (int i = 0; i < height; i++)  // Loop runs for the height of the rectangle
        {
            // Loop to print the number across the width of the rectangle
            for (int j = 0; j < width; j++)  // Loop runs for the width of the rectangle
            {
                Console.Write(number);  // Display the number at the current position
            }
            Console.WriteLine();  // Move to the next line after printing one row of the rectangle
        }
    }
}

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

  •  Repetitive structures

    This C# exercise aims to develop a program that prompts the user for two numbers and displays all the numbers between them (inclusive) three times using for, while...

  •  Digits in a number

    This C# exercise aims to teach how to develop a program that calculates the number of digits in a positive integer. The program uses an efficient technique by perform...

  •  Hollow square

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

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