VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-print-diamond-shape/

⇱ Print the Diamond Shape - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print the Diamond Shape

Last Updated : 11 Mar, 2026

Given a number n, print a diamond-shaped star pattern with 2n rows, where the number of stars first increases and then decreases to form the diamond.

Examples :

Input: 5

Output:

👁 frame_3345

Using Nested Loops - O(n²) Time and O(1) Space

The diamond pattern can be generated by printing two triangular patterns. The first part prints the upper half where the number of stars increases in each row while the spaces decrease. The second part prints the lower half where the number of stars decreases and the spaces increase. Nested loops are used to print spaces and stars in each row.

Step-by-Step Approach:

  • Initialize a variable space = n - 1 to track the number of spaces before stars.
  • Run a loop from i = 0 to n - 1 to print the upper half of the diamond.
  • Print space number of spaces using a loop.
  • Print i + 1 stars separated by spaces.
  • Decrease space after each row.
  • Reset space = 0 for the lower half of the diamond.
  • Run another loop from i = n down to 1.
  • Print space number of spaces.
  • Print i stars separated by spaces.
  • Increase space after each row.

Output
 * 
 * * 
 * * * 
 * * * * 
* * * * * 
* * * * * 
 * * * * 
 * * * 
 * * 
 * 

Using Recursion - O(N²) Time and O(N) Space

In this approach, recursion is used to print spaces and stars for each row of the diamond. Separate recursive functions print blank spaces, stars, the upper half, and the lower half of the diamond.


Output
 
 * 
 * * 
 * * * 
 * * * * 
* * * * * 
* * * * * 
 * * * * 
 * * * 
 * * 
 * 
 
Comment