AlgoInsight

DSA problem

Pyramid of Stars

EasyPattern PrintingPattern: Nested LoopsbeginnerFree

Given an integer N, print a centered pyramid of stars with N rows.

A pyramid is not just about stars - it is about printing the right number of leading spaces so every row is centered. For row i you need N - i leading spaces followed by 2 * i - 1 stars. That formula makes the top a single star and the base 2 * N - 1 stars wide.

Once you internalize the space-versus-star relationship, every symmetric pattern becomes a variation of this one. This is the gateway to alignment and formatting logic used well beyond pattern printing.

Examples

Example 1
Input: N = 5
Output: * *** ***** ******* *********
Explanation: Row i has (N - i) leading spaces and (2 * i - 1) stars, so the shape is centered.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

patternloops

Browse all DSA problems