Square - C# Programming Exercise

This C# exercise aims to create a program that prompts the user to enter a number and a width, then displays a square with the given width, using that number as the inner symbol. The program should display a grid of numbers, where each row contains the number entered by the user repeated as many times as specified by the width. This type of exercise is useful for practicing the use of for loops and user input handling, allowing the programmer to get familiar with creating patterns and basic repetition logic. In this program, the first loop will be responsible for creating the rows of the square, and the second loop will place the corresponding numbers in each row, repeating the action as many times as specified in the width. The output will be a series of rows formed by the entered number, creating a perfect square on the console.

 Category

Flow Control

 Exercise

Square

 Objective

Write a C# program that prompts the user to enter a number and a width, and displays a square of that width, using that number for the inner symbol, as shown in this example:

Enter a number: 4
Enter the desired width: 3

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()
    {
        // Prompting the user to enter a number to use as the symbol
        Console.Write("Enter a number: ");
        int number = int.Parse(Console.ReadLine()); // Reading the user's input and parsing it to an integer

        // Prompting the user to enter the desired width of the square
        Console.Write("Enter the desired width: ");
        int width = int.Parse(Console.ReadLine()); // Reading the user's input and parsing it to an integer

        // Outer loop for the rows of the square (height of the square)
        for (int i = 0; i < width; i++)
        {
            // Inner loop for printing the number in each column of the square
            for (int j = 0; j < width; j++)
            {
                // Displaying the number in the current position
                Console.Write(number);
            }

            // Moving to the next line after completing a row
            Console.WriteLine();
        }
    }
}

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

  •  Break & continue

    This C# exercise aims to develop a program that displays the even numbers from 10 to 20, both inclusive, except for 16, using three different approaches...

  •  Rectangle V2

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

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