VOOZH about

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

⇱ Program to print solid and hollow rhombus patterns - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to print solid and hollow rhombus patterns

Last Updated : 20 Apr, 2023

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

Input : n = 4
Output : 
Solid Rhombus:
 ****
 ****
 ****
****

Hollow Rhombus:
 ****
 * *
 * *
****

1. Solid Rhombus : Making Solid Rhombus is a bit similar to making solid square rather than the concept that for each ith row we have n-i blank spaces before stars as: 
- - - **** 
- - **** 
- **** 
**** 
2. Hollow Rhombus : Formation of Hollow Rhombus uses the idea behind formation of hollow square and solid rhombus. A hollow square with n-i blank spaces before stars in each ith rows result in formation of hollow rhombus. 
 


Output
Solid Rhombus:
 *****
 *****
 *****
 *****
*****

Hollow Rhombus:
 *****
 * *
 * *
 * *
*****

Time Complexity: O(r2), where r represents the given number of rows.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Approach:

solidRhombus function:

  • Initialize variables i and j.
  • Loop from i = 1 to n:
  • Loop from j = 1 to n - i and print spaces.
  • Loop from j = 1 to n and print asterisks.
    • Print a new line.
  • End of function.

hollowRhombus function:

  • Initialize variables i and j.
  • Loop from i = 1 to n:
  • Loop from j = 1 to n - i and print spaces.
  • Loop from j = 1 to n:
  • If i = 1 or i = n or j = 1 or j = n, print an asterisk.
  • Else, print a space.
    • Print a new line.
  • End of function.

Output
Solid Rhombus:
 *****
 *****
 *****
 *****
*****
Hollow Rhombus:
 *****
 * *
 * *
 * *
*****

The time complexity of both functions is O(n^2), because there are two nested loops that iterate over all the rows and columns of the rhombus.
The space complexity is O(1), because only a constant amount of memory is used regardless of the size of the rhombus.


 

Comment