Shape Rule
m² per value
Row 1 prints 1, row 2 prints 4 9 16, row 3 prints five squares — each value is the next perfect square.

The square number pyramid prints 1, then 4 9 16, then 25 36 49 64 81, … — a natural step after Program 40’s alternating 1/0 pattern. This tutorial covers odd-length rows, indentation centering, a running counter m, fixed-width formatting, a live preview, worked C# examples, edge cases, and complexity.
m² per value
Row 1 prints 1, row 2 prints 4 9 16, row 3 prints five squares — each value is the next perfect square.
i = 1, 3, 5…
for (i = 1; i <= 9; i += 2) — odd values set how many squares print per row.
Center rows
for (j = i; j < 9; j++) prints leading spaces so the pyramid stays centered.
Running sequence
Print m*m, then m++ — squares progress 1, 4, 9, 16, 25, … continuously.
2–5 levels
Pick a level count and draw the square-number pyramid in the browser.
Complexity
Total square prints = n² for n levels; extra memory stays O(1).
A square number pyramid prints perfect squares in centered rows of odd length — 1, then 3, then 5 squares per row. With five levels, the output starts with 1, then 4 9 16, then 25 36 49 64 81, and continues.
In C# the outer loop steps i by 2, an indent loop prints leading spaces, and an inner loop prints {0,4}-formatted m*m values while incrementing m.
It combines nested loops with math and formatted output — a key step after Program 40’s alternating rows.
Each row prints i squares.
Indent loop shifts narrow rows right.
Program 40 alternates 1/0; Program 41 prints perfect squares.
Follow Program 40; continue to Program 42 next.
In short: outer i += 2, indent spaces, inner print {0,4} of m*m, then m++ and WriteLine().
Given a level count (e.g. 5 odd-width rows), print a centered pyramid of perfect squares using a running counter m and fixed-width columns.
// 5 levels (i = 1, 3, 5, 7, 9)
// 1
// 4 9 16
//25 36 49 64 81
//... | Item | Type | Description |
|---|---|---|
levels | int | Number of pyramid rows — outer loop uses odd i up to 2*levels - 1. |
i | int | Outer loop — odd row width (1, 3, 5, …); also drives indent count. |
m | int | Running counter — each printed value is m*m, then m++. |
set m = 1
for i from 1 to maxWidth step 2:
print (maxWidth - i) pairs of spaces
repeat i times:
print m*m with fixed width
m++
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops + counter | 1, 4 9 16, … | Learning and interviews |
| User-input levels | maxWidth = 2*levels - 1 | Flexible console programs |
| Left-aligned | Skip indent loop | Easier tracing on paper |
| Goal | Pattern |
|---|---|
| Walk odd rows | for (i = 1; i <= 9; i += 2) |
| Indent spaces | for (j = i; j < 9; j++) Console.Write(" "); |
| Print squares | Console.Write("{0,4}", m * m); m++; |
| End the row | Console.WriteLine(); |
| User input | int.TryParse(Console.ReadLine(), out levels) |
| Program 40 contrast | Alternating 1/0 with shrinking rows — no m*m |
Same square-number pyramid — different ways to control size and alignment.
i += 2Odd row widths
j = i..max-1Centers the pyramid
m*mPerfect squares
{0,4}Fixed-width columns
Reach for this pattern when teaching formatted output, running counters, and centered pyramids.
Natural follow-up after Program 40 — same nested-loop skills but adds math and column alignment.
Outer/inner bound practice with an immediate visual check.
Combine loops with ReadLine for a flexible row count.
Compare Program 40 (alternating 1/0) and Program 42 (next in series) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose a level count between 2 and 5 and draw the square-number pyramid in the browser.
Three complete C# programs — fixed rows, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print five levels of the square-number pyramid with nested loops and formatted output.
Hard-coded pyramid — ideal for first demos and screenshots.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j, k;
int m = 1;
for (i = 1; i <= 9; i += 2)
{
for (j = i; j < 9; j++)
Console.Write(" ");
for (k = 1; k <= i; k++)
{
Console.Write("{0,4}", m * m);
m++;
}
Console.WriteLine();
}
}
}
} When i = 1, one square prints — 1. When i = 3, three squares print — 4 9 16 (from m = 2, 3, 4). The indent loop shifts narrow rows right so the pyramid stays centered.
Read the level count from the console instead of hard-coding five rows.
Read levels from the console with safe parsing.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int levels;
int m = 1;
Console.Write("Enter number of levels: ");
if (!int.TryParse(Console.ReadLine(), out levels) || levels < 1) return;
int maxWidth = 2 * levels - 1;
for (int i = 1; i <= maxWidth; i += 2)
{
for (int j = i; j < maxWidth; j++)
Console.Write(" ");
for (int k = 1; k <= i; k++)
{
Console.Write("{0,4}", m * m);
m++;
}
Console.WriteLine();
}
}
}
} Same inner-loop core as Example 1; only the source of rows changes from a literal to user input.
Skip the indent loop to print squares flush left — easier to trace on paper.
Same squares and counter — no leading spaces.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int m = 1;
for (int i = 1; i <= 9; i += 2)
{
for (int k = 1; k <= i; k++)
{
Console.Write("{0,4}", m * m);
m++;
}
Console.WriteLine();
}
}
}
} Only the indent loop is removed — m*m and {0,4} formatting stay the same as Example 1. Rows grow wider to the right without centering.
using System; brings in Console. Set m = 1 and loop variables i, j, k for 5 levels.
for (i = 1; i <= 9; i += 2) — odd values 1, 3, 5, 7, 9 set how many squares print per row.
for (j = i; j < 9; j++) — prints leading spaces so narrow rows stay centered.
Console.Write("{0,4}", m * m); m++; — fixed-width perfect squares in sequence.
Console.WriteLine() ends the row after the print loop finishes.
Total prints for 5 levels = 1+3+5+7+9 = 25 — O(n²) time, O(1) extra memory.
Trace each outer-loop value of i, indent count, square count, m range, and row output.
i | Spaces | Squares | m range | Values |
|---|---|---|---|---|
1 | 8 pairs | 1 | 1 | 1 |
3 | 6 pairs | 3 | 2–4 | 4 9 16 |
5 | 4 pairs | 5 | 5–9 | 25 36 49 64 81 |
7 | 2 pairs | 7 | 10–16 | 100 121 144 … 256 |
9 | 0 pairs | 9 | 17–25 | 289 324 … 625 |
Squares per row = i — total prints = 1+3+5+7+9 = 25 = 5² for 5 levels.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change {0,4} to {0,5} when squares exceed 999.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 42 for the next pattern in the series.
Practice Write vs WriteLine without complex math.
Example: put WriteLine inside the inner loop by mistake.
Add spaces or wider field width once the three-loop structure works.
Example: use Console.Write("{0,5}", m * m) for larger pyramids.
Triangular totals make O(n²) concrete for beginners.
Example: count printed squares for 5 levels — total is 25 (5²).
Pair the pattern with TryParse and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C# courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Change to cubes, widen columns, or add more levels with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i, m, and indent count on paper for 3 levels before coding — watch how row width grows by 2 each time.
Small habits that keep number-pattern code clean.
Outer loop steps by 2 (i += 2); indent bound must equal the maximum i.
TryParseAvoid crashes when the user types letters instead of a number.
Only call WriteLine() after the inner loop finishes the row.
Console.Write("{0,4}", m * m) keeps columns aligned as values grow from 1 to 625.
Trace i = 1, 3, 5 on paper before coding the full 5-level demo.
Pro Tip: if the output is a vertical list of single squares per line, you almost certainly put WriteLine inside the print loop.
Mistakes that commonly break square-number pyramids.
Each square lands on its own line — you get a column, not a pyramid.
→ Use Write("{0,4}", m*m) for squares; WriteLine only after the print loop.
Printing m*m without m++ repeats the same square on every column.
→ Call m++ after each printed square inside the inner loop.
Indent loop bound must match the outer maximum (9 for 5 levels, or maxWidth in Example 2).
→ Use for (j = i; j < maxWidth; j++) with maxWidth = 2*levels - 1.
{0,4} breaks alignment when squares reach 1000+ — columns overlap.
→ Increase to {0,5} or {0,6} for larger pyramids.
Letters or empty input throw FormatException.
→ Prefer int.TryParse and re-prompt on failure.
Check these inputs before calling the solution done.
One level prints just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: centered 1 and 4 9 16.
Convert.ToInt32 throws — use TryParse.
Each row prints i squares — total work grows as n² for n levels.
Try these variations to lock in the pattern.
m*m to m*m*mi = 1, 3, 5…. Indent: j = i..max-1. Print: k = 1..i with m*m.Console.Write stays on the line; WriteLine advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.i prints exactly i squares — total for n levels = n² (sum of first n odd numbers).Quick Takeaway: outer i += 2, indent spaces, print {0,4} of m*m with m++, then WriteLine().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The square number pyramid is a compact nested-loop lesson: odd-width rows, indentation centering, a running counter m, and fixed-width m*m output. Master the fixed-level version, then try user input and the left-aligned variant.
Practice the three examples above, then continue to Program 42 for the next pattern in the series.
Row i prints i squares from m*m — keep WriteLine outside the print loop and match indent bound to maxWidth.
for (i = 1; i <= maxWidth; i += 2) in the outer loopfor (j = i; j < maxWidth; j++) prints leading spaces{0,4} of m*m and increment m each timelevels ≥ 1 for interactive programsint.TryParse over bare Convert.ToInt32WriteLine inside the square-print loopm++ after each printed squarerows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints i squares
Definitioni += 2 (odd widths)
Codem*m then m++
Code{0,4} alignment
OutputO(n²) time
AnalysisEach printed value is m² from a running counter m. Row widths are odd (1, 3, 5, 7, 9) — total prints for n levels = n².
Move on to the next pattern in the C# number-pattern series.
12 people found this page helpful