VOOZH about

URL: https://www.geeksforgeeks.org/dsa/printing-pyramid-patterns/

⇱ Printing Pyramid Patterns - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Printing Pyramid Patterns

Last Updated : 11 Mar, 2026

Given a positive integer n, print a pyramid pattern consisting of stars (*) such that the number of rows equals n. The pyramid should be center-aligned as shown in the example below.

Example:

Input: n = 5
Output:

👁 5-row-diamond

Explanation: Here, the total number of rows is 5, which forms a pyramid structure.

Input: n = 3
Output:

👁 3-row-diamond

Input: n = 7
Output:

👁 7-row-diamond

Using Two Loops - O( n^2) Time and O(1) Space

The idea is to use two nested loops. The outer loop is used to track the rows, while the inner loops are used to print the required spaces and stars for each row.

Illustration:

  • For every row i (from 1 to n), the pyramid must remain center aligned.
  • First print spaces, then print stars (*).
  • The number of leading spaces for row i is n - i, which decreases as the row increases.
  • After spaces, print 2 × i − 1 stars.
  • This pattern makes the pyramid grow symmetrically from the center.
Comment
Article Tags: