Placement Prep

Pyramid Star Patterns in C, C++, Java, and Python

Six pyramid star patterns with C, C++, Java, and Python code: right-angled, centered, inverted, hollow, and diamond. Loop logic and sample output for n=5.

By FACE Prep Team 11 min read
star-pattern pyramid-pattern c-programming java python placement-prep pattern-programs

Pyramid star patterns test two skills placement coding rounds check immediately: loop control and condition logic.

Each pattern here reduces to counting how many spaces and how many stars belong on each row. Derive those counts from the row number and the total height, write the two inner loops, and the shape renders correctly. Get the counts wrong and you have a different shape with no partial credit.

Six standard variations are covered below, from the simplest right-angled triangle to the hollow pyramid that adds a boundary condition to the inner loop. For number-based pyramid variants, see the companion article on palindrome pyramid patterns using numbers, which covers the ascending-then-descending number rows that appear alongside star patterns in placement assessments. This article stays entirely with asterisk patterns.

Why Pattern Programs Show Up in Placement Rounds

Pattern programs are a recurring category in the coding sections of Indian fresher assessments because they expose something subtle: whether a candidate can translate a visual structure into quantitative loop bounds. The output is either correct or it is not. There is no partial credit for approximately the right shape.

The task is also time-bounded. Most placement coding sessions run 30 to 45 minutes with three to five questions. A candidate who has never traced through a centered pyramid will spend the first quarter of that time on the space count alone. One who already knows the formula (n - i spaces and 2 * i - 1 stars) moves on in three minutes and banks time for the harder questions that follow.

Pattern problems also sit at the bottom of the difficulty curve, which makes them useful warm-up problems. They separate candidates who have practised structured decomposition from those who rely on memorisation. For an overview of which coding skills companies actually weight across placement roles, see IT jobs for freshers.

Pattern Formulas at a Glance

All six patterns share the same outer loop structure: i runs from 1 to n. The difference lies in the inner-loop counts. Variable i refers to the current row (1-indexed from the top) and n is the total height.

PatternLeading spaces per rowStars printed per row
Right-angled0i
Mirrored right-angledn - ii
Centered fulln - i2i - 1
Inverted fulln - i (outer loop runs n to 1)2i - 1
Hollown - i2i - 1 with interior spaces
Diamondn - i (upper then lower pass)2i - 1

The inverted full pyramid uses the identical per-row formula as the centered pyramid. The only change is running the outer loop in reverse.

Right-Angled Pyramid

Row i prints exactly i stars with no leading spaces. The inner loop runs from 1 to i. This is the baseline nested-loop pyramid: one outer loop to step through rows and one inner loop to print the stars.

Expected output for n = 5:

*
**
***
****
*****

C:

#include <stdio.h>
int main() {
    int n = 5, i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++)
            printf("*");
        printf("\n");
    }
    return 0;
}

C++:

#include <iostream>
using namespace std;
int main() {
    int n = 5;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            cout << "*";
        cout << "\n";
    }
    return 0;
}

Java:

public class RightPyramid {
    public static void main(String[] args) {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Python:

n = 5
for i in range(1, n + 1):
    print('*' * i)

Python’s range() function counts from 1 to n inclusive when written as range(1, n + 1). String multiplication '*' * i produces i copies of the character and removes the need for a separate inner loop.

Mirrored Right-Angled Pyramid

The mirrored variant aligns the staircase to the right edge by prefixing each row with n - i spaces. Row i still prints i stars. As i grows, the leading-space count shrinks and the star count grows, so the right edge stays flush across all rows.

Expected output for n = 5:

    *
   **
  ***
 ****
*****

C:

#include <stdio.h>
int main() {
    int n = 5, i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++)
            printf(" ");
        for (j = 1; j <= i; j++)
            printf("*");
        printf("\n");
    }
    return 0;
}

C++:

#include <iostream>
using namespace std;
int main() {
    int n = 5;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= i; j++)
            cout << "*";
        cout << "\n";
    }
    return 0;
}

Java:

public class MirroredPyramid {
    public static void main(String[] args) {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++)
                System.out.print(" ");
            for (int j = 1; j <= i; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Python:

n = 5
for i in range(1, n + 1):
    print(' ' * (n - i) + '*' * i)

The Python one-liner states the formula directly: (n - i) spaces concatenated with i stars. Tracing it for i = 1 gives 4 spaces and 1 star; for i = 5 gives 0 spaces and 5 stars, confirming both edges of the output in two lines of mental arithmetic.

Centered Full Pyramid

The centered pyramid is the shape most people picture when asked to draw a star pyramid. Row i has n - i leading spaces and 2 * i - 1 stars. Row 1 has 1 star. The star count increases by 2 each row. Row n has 2 * n - 1 stars, which is 9 for n = 5. The total star count across all rows is n squared (25 for n = 5).

Expected output for n = 5:

    *
   ***
  *****
 *******
*********

C:

#include <stdio.h>
int main() {
    int n = 5, i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++)
            printf(" ");
        for (j = 1; j <= 2 * i - 1; j++)
            printf("*");
        printf("\n");
    }
    return 0;
}

C++:

#include <iostream>
using namespace std;
int main() {
    int n = 5;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= 2 * i - 1; j++)
            cout << "*";
        cout << "\n";
    }
    return 0;
}

Java:

public class CenteredPyramid {
    public static void main(String[] args) {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++)
                System.out.print(" ");
            for (int j = 1; j <= 2 * i - 1; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Python:

n = 5
for i in range(1, n + 1):
    print(' ' * (n - i) + '*' * (2 * i - 1))

The 2 * i - 1 formula is worth deriving once rather than memorising. The centered pyramid grows by two stars per row from a single star at the apex. Row 1 contributes 2(1) - 1 = 1 star; row 5 contributes 2(5) - 1 = 9 stars. The pattern is the sum of the first n odd numbers, which always equals n squared.

Inverted Pyramid

The inverted pyramid uses the identical per-row formula as the centered version. The only change is the outer loop direction: i starts at n and decrements to 1. Row printed first (i = n) has 0 leading spaces and 2 * n - 1 stars. Each subsequent row adds one leading space and loses two stars.

Expected output for n = 5:

*********
 *******
  *****
   ***
    *

C:

#include <stdio.h>
int main() {
    int n = 5, i, j;
    for (i = n; i >= 1; i--) {
        for (j = 1; j <= n - i; j++)
            printf(" ");
        for (j = 1; j <= 2 * i - 1; j++)
            printf("*");
        printf("\n");
    }
    return 0;
}

C++:

#include <iostream>
using namespace std;
int main() {
    int n = 5;
    for (int i = n; i >= 1; i--) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= 2 * i - 1; j++)
            cout << "*";
        cout << "\n";
    }
    return 0;
}

Java:

public class InvertedPyramid {
    public static void main(String[] args) {
        int n = 5;
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= n - i; j++)
                System.out.print(" ");
            for (int j = 1; j <= 2 * i - 1; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Python:

n = 5
for i in range(n, 0, -1):
    print(' ' * (n - i) + '*' * (2 * i - 1))

Java’s for loop handles a downward count with the same three-part syntax: for (int i = n; i >= 1; i--). Python uses range(n, 0, -1) for the equivalent step. The leading-space and star-count formulas are identical to the centered pyramid; only the traversal direction differs.

Hollow Pyramid

The hollow pyramid uses the centered-pyramid frame but fills the interior with spaces. The star-or-space decision moves into a conditional inside the inner loop:

  • Print * when j == 1 (left edge of the row).
  • Print * when j == 2 * i - 1 (right edge of the row).
  • Print * when i == n (base row, every position filled).
  • Print a space otherwise.

Row 1 is a single position where both edge conditions coincide: j == 1 and j == 2 * 1 - 1 == 1. So a single star prints correctly at the apex without any special case. Rows 2 through n - 1 show two stars with interior spaces expanding by two per row. The base row fills all 2 * n - 1 positions.

Expected output for n = 5:

    *
   * *
  *   *
 *     *
*********

C:

#include <stdio.h>
int main() {
    int n = 5, i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++)
            printf(" ");
        for (j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1 || i == n)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

C++:

#include <iostream>
using namespace std;
int main() {
    int n = 5;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1 || i == n)
                cout << "*";
            else
                cout << " ";
        }
        cout << "\n";
    }
    return 0;
}

Java:

public class HollowPyramid {
    public static void main(String[] args) {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++)
                System.out.print(" ");
            for (int j = 1; j <= 2 * i - 1; j++) {
                if (j == 1 || j == 2 * i - 1 || i == n)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Python:

n = 5
for i in range(1, n + 1):
    row = ''
    for j in range(1, 2 * i):
        if j == 1 or j == 2 * i - 1 or i == n:
            row += '*'
        else:
            row += ' '
    print(' ' * (n - i) + row)

A common off-by-one in Python: writing range(1, 2 * i - 1) for the inner loop stops one position short of the right edge. The correct upper bound is range(1, 2 * i), which produces values 1 through 2 * i - 1 inclusive. Verify by tracing i = 2: the loop should reach j = 3 (the right edge), not stop at j = 2.

Diamond Pattern

A diamond is two pyramids joined at the widest row. Print the centered pyramid from row 1 to n, then continue with the inverted version from row n - 1 down to 1. The base row (i = n) is printed exactly once, so the total row count is 2 * n - 1. For n = 5 that gives 9 rows.

Expected output for n = 5:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

C:

#include <stdio.h>
int main() {
    int n = 5, i, j;
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++) printf(" ");
        for (j = 1; j <= 2 * i - 1; j++) printf("*");
        printf("\n");
    }
    for (i = n - 1; i >= 1; i--) {
        for (j = 1; j <= n - i; j++) printf(" ");
        for (j = 1; j <= 2 * i - 1; j++) printf("*");
        printf("\n");
    }
    return 0;
}

C++:

#include <iostream>
using namespace std;
int main() {
    int n = 5;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++) cout << " ";
        for (int j = 1; j <= 2 * i - 1; j++) cout << "*";
        cout << "\n";
    }
    for (int i = n - 1; i >= 1; i--) {
        for (int j = 1; j <= n - i; j++) cout << " ";
        for (int j = 1; j <= 2 * i - 1; j++) cout << "*";
        cout << "\n";
    }
    return 0;
}

Java:

public class Diamond {
    public static void main(String[] args) {
        int n = 5;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++) System.out.print(" ");
            for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
            System.out.println();
        }
        for (int i = n - 1; i >= 1; i--) {
            for (int j = 1; j <= n - i; j++) System.out.print(" ");
            for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
            System.out.println();
        }
    }
}

Python:

n = 5
for i in range(1, n + 1):
    print(' ' * (n - i) + '*' * (2 * i - 1))
for i in range(n - 1, 0, -1):
    print(' ' * (n - i) + '*' * (2 * i - 1))

Because the upper and lower halves share the same per-row formula, extracting a helper function keeps the code clean. In a live coding interview, showing that refactored version after the working solution demonstrates awareness of code reuse without over-engineering the first attempt.

The hollow pyramid’s three-condition boundary check (j == 1, j == 2 * i - 1, i == n) is a precise constraint specification. Describing those constraints clearly in a prompt is enough for an LLM to generate the correct loop in any language. TinkerLLM at ₹299 is a prompt-engineering environment where you can test that claim directly: write the boundary rules as a specification, compare the generated code against the 9-row diamond output, and iterate until the output matches exactly. The pattern derivations in this article give you the ground truth to judge the model’s output.

Primary sources

Frequently asked questions

What is the loop formula for a centered pyramid pattern?

For height n, row i needs (n-i) leading spaces and (2i-1) stars. Row 1 gives 1 star; row 5 gives 9 stars when n=5.

How do I print a hollow pyramid in C?

Use a nested loop. Print a star when j==1, when j==2*i-1 (rightmost position in that row), or when i==n (base row). All other positions get a space character.

What is the difference between a right-angled and a centered pyramid pattern?

A right-angled pyramid adds one star per row from a left edge, with no leading spaces. A centered pyramid adds two stars per row and uses leading spaces to keep the apex centered above the base.

How does a diamond pattern work?

A diamond is a centered pyramid from row 1 to n, followed by the same pyramid inverted from row n-1 down to row 1. Total rows equal 2n-1, giving 9 rows for n=5.

Do pattern programs appear in placement coding tests?

Pattern-printing problems appear in the coding sections of most Indian fresher placement assessments. They test loop control, condition logic, and output formatting under timed conditions.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF