VOOZH about

URL: https://www.geeksforgeeks.org/computer-science-fundamentals/program-to-print-solid-and-hollow-square-patterns/

⇱ Program to print solid and hollow square patterns - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to print solid and hollow square patterns

Last Updated : 13 Mar, 2023

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. 
 

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.


 

Comment