GFG App
Open App
Browser
Continue

# Program to print solid and hollow square patterns

For any given number n, print Hollow and solid Squares and Rhombus made with stars(*).
Examples:

```Input : n = 4
Output :

Solid Square:
****
****
****
****

Hollow Square:
****
*  *
*  *
****```

1. Solid Square : Solid Square is easiest among all given patterns. To print Solid square with n rows, we should use two loops iterating n times both. Where the outer loop is used for numbers of rows and the inner loop is used for printing all stars in a particular row.
2. Hollow Square : Hollow Square requires a little bit improvisation. Here we should again use two loops, where 1st and last row must have all n-stars and remaining rows have stars only in 1st and last column.

## C++

 `// C++ program to print  ` `// hollow and solid square patterns ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function for hollow square ` `void` `hollowSquare(``int` `rows) ` `{ ` `    ``int` `i, j; ` `    ``for` `(i=1; i<=rows; i++) ` `    ``{ ` `        ``// Print stars for each solid rows ` `        ``if` `(i==1 || i==rows) ` `            ``for` `(j=1; j<=rows; j++) ` `                ``cout << ``"*"``; ` ` `  `        ``// stars for hollow rows ` `        ``else` `            ``for` `(j=1; j<=rows; j++) ` `                ``if` `(j==1 || j==rows) ` `                    ``cout << ``"*"``; ` `                ``else` `                    ``cout << ``" "``; ` ` `  `        ``// Move to the next line/row ` `        ``cout << ``"\n"``; ` `    ``} ` `} ` ` `  `// Function for Solid square ` `void` `solidSquare(``int` `rows) ` `{ ` `    ``int` `i, j; ` `    ``for` `(i=1; i<=rows; i++) ` `    ``{ ` `        ``// Print stars after spaces ` `        ``for` `(j=1; j<=rows; j++) ` `            ``cout << ``"*"``; ` ` `  `        ``// Move to the next line/row ` `        ``cout << ``"\n"``; ` `    ``} ` ` `  `} ` ` `  `// Utility program to print all patterns ` `void` `printPattern(``int` `rows) ` `{ ` `    ``cout << ``"\nSolid Square:\n"``; ` `    ``solidSquare(rows); ` `     `  `    ``cout << ``"\nHollow Square:\n"``;  ` `    ``hollowSquare(rows); ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `rows = 5; ` `    ``printPattern (rows); ` `    ``return` `0; ` `} `

## Java

 `// Java program to print  ` `// hollow and solid square patterns ` ` `  `class` `GFG  ` `{ ` `    ``// Function for hollow square ` `    ``static` `void` `hollowSquare(``int` `rows) ` `    ``{ ` `        ``int` `i, j; ` `        ``for` `(i = ``1``; i <= rows; i++) ` `        ``{ ` `            ``// Print stars for each solid rows ` `            ``if` `(i == ``1` `|| i == rows) ` `                ``for` `(j = ``1``; j <= rows; j++) ` `                    ``System.out.print(``"*"``); ` `     `  `            ``// stars for hollow rows ` `            ``else` `                ``for` `(j = ``1``; j <= rows; j++) ` `                    ``if` `(j == ``1` `|| j == rows) ` `                        ``System.out.print(``"*"``); ` `                    ``else` `                        ``System.out.print(``" "``); ` `     `  `            ``// Move to the next line/row ` `            ``System.out.print(``"\n"``); ` `        ``} ` `    ``} ` `     `  `    ``// Function for Solid square ` `    ``static` `void` `solidSquare(``int` `rows) ` `    ``{ ` `        ``int` `i, j; ` `        ``for` `(i = ``1``; i <= rows; i++) ` `        ``{ ` `            ``// Print stars after spaces ` `            ``for` `(j = ``1``; j <= rows; j++) ` `                ``System.out.print(``"*"``); ` `     `  `            ``// Move to the next line/row ` `            ``System.out.print(``"\n"``); ` `        ``} ` `     `  `    ``} ` `     `  `    ``// Utility program to print all patterns ` `    ``static` `void` `printPattern(``int` `rows) ` `    ``{ ` `        ``System.out.print(``"\nSolid Square:\n"``); ` `        ``solidSquare(rows); ` `         `  `        ``System.out.print(``"\nHollow Square:\n"``);  ` `        ``hollowSquare(rows); ` `    ``} ` `     `  `    ``// Driver program  ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `rows = ``5``; ` `        ``printPattern (rows); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python3 program to print ` `# hollow and solid square patterns ` ` `  `# Function for hollow square ` `def` `hollowSquare(rows): ` ` `  `    ``for` `i ``in` `range``(``1``, rows ``+` `1``): ` `         `  `        ``# Print stars for each solid rows ` `        ``if` `(i ``=``=` `1` `or` `i ``=``=` `rows): ` `            ``for` `j ``in` `range``(``1``, rows ``+` `1``): ` `                ``print``(``"*"``, end ``=` `"") ` ` `  `        ``# stars for hollow rows ` `        ``else``: ` `            ``for` `j ``in` `range``(``1``, rows ``+` `1``): ` `                ``if` `(j ``=``=` `1` `or` `j ``=``=` `rows): ` `                    ``print``(``"*"``, end ``=` `"") ` `                ``else``: ` `                    ``print``(end ``=` `" "``) ` ` `  `        ``# Move to the next line/row ` `        ``print``() ` `     `  `# Function for Solid square ` `def` `solidSquare(rows): ` ` `  `    ``for` `i ``in` `range``(``1``, rows): ` `         `  `        ``# Print stars after spaces ` `        ``for` `j ``in` `range``(``1``, rows ``+` `1``): ` `            ``print``(``"*"``, end ``=` `"") ` ` `  `        ``# Move to the next line/row ` `        ``print``() ` `     `  `# Utility program to print all patterns ` `def` `printPattern(rows): ` ` `  `    ``print``(``"Solid Square:"``) ` `    ``solidSquare(rows) ` `     `  `    ``print``(``"\nHollow Square:"``)  ` `    ``hollowSquare(rows) ` ` `  `# Driver Code ` `rows ``=` `5` `printPattern (rows) ` ` `  `# This code is contributed by  ` `# Mohit kumar 29 `

## C#

 `// C# program to print ` `// hollow and solid square patterns ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``// Function for hollow square ` `    ``static` `void` `hollowSquare(``int` `rows) ` `    ``{ ` `        ``int` `i, j; ` `        ``for` `(i = 1; i <= rows; i++) ` `        ``{ ` `            ``// Print stars for each solid rows ` `            ``if` `(i == 1 || i == rows) ` `                ``for` `(j = 1; j <= rows; j++) ` `                    ``Console.Write(``"*"``); ` `     `  `            ``// stars for hollow rows ` `            ``else` `                ``for` `(j = 1; j <= rows; j++) ` `                    ``if` `(j == 1 || j == rows) ` `                        ``Console.Write(``"*"``); ` `                    ``else` `                        ``Console.Write(``" "``); ` `     `  `            ``// Move to the next line/row ` `            ``Console.WriteLine(); ` `        ``} ` `    ``} ` `     `  `    ``// Function for Solid square ` `    ``static` `void` `solidSquare(``int` `rows) ` `    ``{ ` `        ``int` `i, j; ` `        ``for` `(i = 1; i <= rows; i++) ` `        ``{ ` `            ``// Print stars after spaces ` `            ``for` `(j = 1; j <= rows; j++) ` `                ``Console.Write(``"*"``); ` `     `  `            ``// Move to the next line/row ` `            ``Console.WriteLine(); ` `        ``} ` `     `  `    ``} ` `     `  `    ``// Utility program to print all patterns ` `    ``static` `void` `printPattern(``int` `rows) ` `    ``{ ` `        ``Console.Write(``"\nSolid Square:\n"``); ` `        ``solidSquare(rows); ` `         `  `        ``Console.Write(``"\nHollow Square:\n"``); ` `        ``hollowSquare(rows); ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `Main () ` `    ``{ ` `        ``int` `rows = 5; ` `        ``printPattern (rows); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

## Javascript

 ``

Output :

```Solid Square:
*****
*****
*****
*****
*****

Hollow Square:
*****
*   *
*   *
*   *
*****```

Time Complexity: O(n2), where n represents the given input.
Auxiliary Space: O(1), no extra space is required, so it is a constant.