Rectangle - C# Programming Exercise

This C# exercise helps you practice using loops and user input. In this program, the user will be asked to input a number (a digit), and the program will display a rectangle that is 3 columns wide and 5 rows tall using that digit. Each row of the rectangle will alternate between digits together and digits separated by spaces, like the following example:

The goal of this exercise is to familiarize yourself with handling user input and how to use a loop to print patterns on the console. This type of exercise is very useful for understanding how to control the layout of elements in a console application using C#.

By completing this exercise, you will enhance your skills with repetitive structures like for and foreach, and learn how to handle user input to generate dynamic outputs on the console.

 Category

First contact with C# Sharp

 Exercise

Rectangle

 Objective

Write a C# program to ask the user for a number and then display a rectangle 3 columns wide and 5 rows tall using that digit. For example:

Enter a digit: 3
333
3 3
3 3
3 3
333

 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()
    {
        int digit; // Declaring a variable to store the digit entered by the user

        // Asking the user to enter a digit and reading the input
        Console.Write("Enter a digit: ");
        digit = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer

        // Printing the top row of the rectangle with the digit repeated three times
        Console.WriteLine("{0}{0}{0}", digit); // Printing the digit three times in the first row

        // Printing the middle rows of the rectangle, each with the digit followed by a space and then the digit again
        for (int i = 0; i < 3; i++) // Loop to print three rows
        {
            Console.WriteLine("{0} {0}", digit); // Printing the digit with a space between them
        }

        // Printing the bottom row of the rectangle with the digit repeated three times
        Console.WriteLine("{0}{0}{0}", digit); // Printing the digit three times in the last row
    }
}

 Share this C# exercise

 More C# Programming Exercises of First contact with C# Sharp

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

  •  Conversion

    This C# exercise teaches you how to perform temperature unit conversions. The program prompts the user to input a temperature in Celsius degrees and then converts it ...

  •  First contact with C#

    This exercise is an excellent way to get started with programming in C#. In this program, you will learn how to print a message on the screen using C#. The program wi...

  •  Sum of two numbers

    This exercise is a great way to practice basic arithmetic operations in C#. In this program, you will learn how to perform an addition operation between two numbers, 12 and ...

  •  Division of two numbers

    This exercise is a great opportunity to practice the division operation in C#. In this program, you will learn how to divide two numbers, in this case, 24 and 5, and ...

  •  Multiple operations and precedences

    This exercise is perfect for learning how to perform complex arithmetic operations in C#. In this program, you will be asked to solve several mathematical operations using d...

  •  Multiply using variables

    This exercise is perfect for learning how to perform a multiplication in C# with numbers entered by the user. In this program, the user will be asked to input two numbers, a...