C# program to print a star square pattern

C# program to print a star square pattern:

In this post, we will learn how to print a star square pattern in C#. The program will take the size of the square as input from the user and print the pattern. We will learn different ways to print the pattern in this post.

C# program to print a star square pattern using a for loop:

Let’s use a for loop to print a star square pattern. This program will use two for loops to print the pattern. It will use the below algorithm:

  • Take the size of the square as input from the user.
  • Run one for loop for the square size number of times. Run another inner for loop for the same number of time.
  • Inside the inner loop, print the character.
  • The outer for loop is used to point to each row of the pattern and the inner for loop is used to print the content of the square.
  • We are using a star, * to print the pattern. But you can use any other character to print it.

Method 1: C# example program by using for loops:

Let’s write down the C# program:

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int Size;

            Console.WriteLine("Enter the size of the square: ");
            Size = Convert.ToInt32(Console.ReadLine());

            for (int row = 0; row < Size; row++)
            {
                for (int column = 0; column < Size; column++)
                {
                    Console.Write("*" + " ");
                }

                Console.WriteLine();
            }

        }
    }
}

Here,

  • The size of the square is stored in the variable Size.
  • The outer for loop runs for Size number of times. The inner for loop also runs for Size number of times.
  • The outer for loop points to each row of the square and the inner for loop points to each item of each row.
  • The star is printed in the inner for loop. We are also adding a blank space to the right of the star to adjust the space as we are printing a new line at the end of each row.
  • On each iteration of the outer for loop completes, it prints a new line to move the control to the next line.

If you run this program, it will print output as like below:

Enter the size of the square: 
5
* * * * * 
* * * * * 
* * * * * 
* * * * * 
* * * * * 

Enter the size of the square: 
10
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 

Method 2: C# program by using while loops:

We can also use while loops to write the same program. The while loops will check the condition and if it is true, it will run the body of the loop. Else, it will move to the next iteration.

namespace Program
{
    class Program
    {
        static void Main(string[] args)
        {
            int Size, row = 0, column;

            Console.WriteLine("Enter the size of the square: ");
            Size = Convert.ToInt32(Console.ReadLine());

            while (row < Size)
            {
                column = 0;
                while (column < Size)
                {
                    Console.Write("*" + " ");
                    column++;
                }

                Console.WriteLine();
                row++;
            }

        }
    }
}
  • row and column variables are used in the loops.
  • row is used in the outer while loop and column is used in the inner while loop.
  • row is initialized to 0 at the start of the program and column is initialized to 0 before the inner while loop starts.
  • The value of row is incremented by 1 at the end of each iteration of the outer while loop and the value of column is incremented by 1 at the end of each iteration of the inner while loop. Both loops runs for Size number of times, which is the size of the square entered by the user.
  • At the end of each iteration of the outer while loop, it prints a new line.

It will give similar output.

C# star square pattern example

You might also like: