C# program to print hollow square or rectangle star(*) pattern – Learn how to print square or rectangle star pattern of length and breadth using for loop in C# programming.
To find the area of rectangle or square we need the length and width values
i.e area of rectangle = length * width.
For square length and width values are same.
i.e if length = s and width=s then area of square = s*s
To print the rectangle Pattern with * we need length and width values.
Input Format
The input will contain 2 integer values.
Input Value
length = 4, width = 5
Output Format
****
* *
* *
* *
****
C# program Logic to print empty square or rectangle star pattern
class Program
{
static void Main(string[] args)
{
/* Input numbers for length and width to construct rectangle or square. */
int length = 4, width = 5;
Console.Write(" ");
for (int i = 1; i <=width ; i++)
{
for (int j = 1; j <= length; j++)
{
if(i==1 ||i==width ||j==1 ||j==length )
{
Console.Write("*");
}
else
{
Console.Write(" ");
}
}
Console.Write("\n ");
}
}
}