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 <bits/stdc++.h> 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): # Prstars 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 = 5printPattern (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
<?php // php program to print hollow // and solid square patterns // Function for hollow square function hollowSquare($rows) { for ($i = 1; $i <= $rows; $i++) { // Print stars for each solid rows if ($i == 1 || $i == $rows) for ($j = 1; $j <= $rows; $j++) echo "*"; // stars for hollow rows else for ($j = 1; $j <= $rows; $j++) if ($j == 1 || $j == $rows) echo "*"; else echo " "; // Move to the next line/row echo "\n"; } } // Function for Solid square function solidSquare($rows) { for ($i = 1; $i <= $rows; $i++) { // Print stars after spaces for ($j = 1; $j <= $rows; $j++) echo "*"; // Move to the next line/row echo "\n"; } } // Utility program to // print all patterns function printPattern($rows) { echo "\nSolid Square:\n"; solidSquare($rows); echo "\nHollow Square:\n"; hollowSquare($rows); } // Driver code $rows = 5; printPattern ($rows); // This code is contributed by mits ?> |
Output :
Solid Square: ***** ***** ***** ***** ***** Hollow Square: ***** * * * * * * *****
This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Program to print solid and hollow rhombus patterns
- Solid square inside a hollow square | Pattern
- Program to print hollow rectangle or square star patterns
- Program to print hollow pyramid and diamond pattern
- Program to print the hollow numerical parallelogram
- Program to print the pattern with two hollow Triangles
- Program to Print Mirrored Hollow Parallelogram
- Program to print hollow Triangle pattern
- Program to print a Hollow Triangle inside a Triangle
- Python Program to print hollow half diamond hash pattern
- Program to print Square inside a Square
- Program to print right and left arrow patterns
- Program to print diagonal star patterns
- Recursive program to print triangular patterns
- Hollow lower triangular pattern
- Minimum number of blocks required to form Hollow Rectangular Prism
- Programs to print Triangle and Diamond patterns using recursion
- Print different star patterns in SQL
- Print Patterns in PL/SQL
- Programs to print Interesting Patterns

