Beginners often memorise twenty pattern programs and still freeze when a coding round changes the shape. The real problem is not the new pattern. It is the missing method. For every row, decide how many spaces to print, then how many values to print and what those values are.
Once those decisions become formulas, star triangles, number pyramids and diamonds stop looking like separate programs. For the centred pyramid the two formulas are spaces(i) = n - i and stars(i) = 2*i - 1, and C, Java and Python differ only in how they print a row.
The one method: rows outside, two decisions inside
Use an outer loop for the row number i, normally from 1 through n. Inside that loop, answer two questions:
How many leading spaces does row
ineed?How many content items does row
ineed, and what should each item contain?
The reusable frame is:
for each row i from 1 to n
print spaces(i) leading spaces
print content_count(i) items using content_rule(i, position)
print a newlineThe outer loop does not change when the shape changes. Only spaces(i), the content count and, for number patterns, the content rule change. Start with 1-based rows because the formulas are easier to read. If your language loop starts at 0, translate the formulas deliberately.
Centred star pyramid for n = 5 in three languages
For a centred pyramid with n = 5, row i uses:
spaces(i) = n - istars(i) = 2*i - 1
The exact output is:
*
***
*****
*******
*********Check the rows before writing code. Row 1 has 5 - 1 = 4 spaces and 2(1) - 1 = 1 star. Row 3 has 5 - 3 = 2 spaces and 2(3) - 1 = 5 stars. Row 5 has 5 - 5 = 0 spaces and 2(5) - 1 = 9 stars. The full width is 9, so the apex of four spaces followed by one star is centred over the base.
Here is the C version:
int n = 5;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) printf(" ");
for (int j = 1; j <= 2 * i - 1; j++) printf("*");
printf("\n");
}The same loop structure in Java is:
int n = 5;
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) System.out.print(" ");
for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}Python can build each row as one string:
n = 5
for i in range(1, n + 1):
print(" " * (n - i) + "*" * (2 * i - 1))All three programs evaluate the same two formulas. Only the printing syntax changes.

Formula table for the common patterns
Treat this table as a translation sheet, not a list of programs to memorise.
Pattern for n rows | spaces(i) | Count and content on row i |
|---|---|---|
Right-angled triangle | 0 |
|
Inverted right triangle | 0 |
|
Centred pyramid |
|
|
Inverted pyramid |
|
|
Diamond | Pyramid rows | Use the matching pyramid formula in each half |
Floyd's triangle | 0 |
|
Pascal's triangle | Alignment depends on formatting | Coefficients |
The diamond needs care at the join. If the upper half already prints the widest row for i = n, begin the lower half at n - 1. Printing n again duplicates the middle row.
When a test gives you an unfamiliar picture, make a three-column row table before coding: i, leading spaces and content count. Fill the first, second and last rows from the picture, then look for the sequence. A count that grows 1, 3, 5, 7 is 2*i - 1; a count that falls 9, 7, 5, 3, 1 across five rows is 2*(n - i) + 1. For that inverted pyramid at n = 5, spaces rise from i - 1 = 0 on row 1 to 4 on row 5, while stars fall from 9 to 1. Checking both endpoints exposes a reversed formula before it reaches the compiler.
When content is a counter or a formula
Stars are simple because every content position holds the same character. Floyd's triangle needs state that continues across rows. For n = 4, start counter = 1, print it, and increment after every number:
1
2 3
4 5 6
7 8 9 10The row counts are 1, 2, 3, 4, so the program prints 1 + 2 + 3 + 4 = 10 numbers. Row 4 starts at 7 because the first three rows consumed 1 + 2 + 3 = 6 values. That gives 7 8 9 10, exactly as required.
Pascal's triangle uses a formula instead of one running counter. In 1-based row i, position r contains the binomial coefficient C(i - 1, r), where r runs from 0 to i - 1. The row count still comes from i; only the content rule changes.
For more loop exercises that transfer across languages, use Classic Programs in C, Java and Python after you can derive these formulas yourself.
C, Java and Python printing gotchas
In C, printf does not add a newline. Print "\n" after the inner loops or every row appears on one line.
In Java, System.out.print stays on the current line, while System.out.println() ends it. Use print inside the row and one println after it.
Python's "*" * k creates k copies in one expression. If you instead print one item at a time, print(value, end="") suppresses the automatic newline. Finish the row with a separate print().
Coding-round traps, exact-output checks and your next step
Most wrong pattern answers are bookkeeping errors:
Using
iwhere the leading-space formula needsn - ishifts the shape.Starting at 0 without changing a 1-based formula creates an empty or oversized row.
Forgetting the row-ending newline places the whole pattern on one line.
Printing an unwanted trailing space can fail an exact-output test even when the shape looks correct.
Coding rounds compare your output with an expected string, so whitespace is part of the answer. Test the first row, a middle row and the last row on paper before you submit. The broader coding-round strategy for placements explains how to protect time for these boundary checks.
The short version is simple: rows outside, then spaces(i) and content inside. Practise that frame in the C Programming course, then use the Placement Preparation catalogue to continue with timed coding work. The KnowledgeGate question bank also carries over 1,000 C-programming questions for drilling control flow, loops and arrays. Do not memorise the next pattern. Derive it.




