How to print Solid/Hollow square pattern in C#

Share Post
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Please Subscribe Youtube| Like Facebook | Follow Twitter

In this tip, we will write two C# methods that will print solid and hollow square pattern 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


Share Post
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Leave a Reply

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