How To Print Solid & Hollow Square Pattern In C# Program

How to print Solid & Hollow square pattern in C#


Please Subscribe Youtube| Like Facebook | Follow Twitter

In this tip, we will write two C# methods that will print Solid & Hollow square pattern in C# respectively.

Solid Square Pattern

private void PrintSolidSquarePattern(int row)
{

    for (int i = 1; i <= row * row; i++)
    {
        if (i % row == 0)
        {
            Console.WriteLine("*");

        }
        else
        {

            Console.Write("*");
                   
        }

    }
}

Hollow Square Pattern

private void PrintHollowSquare(int row)
{
    int currentRow = 1;
    for (int i = 1; i <= row * row ; i++)
    {

        if (i % row == 0)
        {

            Console.WriteLine("*");
            currentRow++;

        }
        else
        {

            if (currentRow <= 1 || i % row == 1 || currentRow == row) {

                Console.Write("*");
            }
            else {

                Console.Write(" ");
            }
        }

    }
}

Please Subscribe Youtube| Like Facebook | Follow Twitter


Leave a Reply

Your email address will not be published. Required fields are marked *