Exercise
Rectangle
Objetive
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
Example Code
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
}
}