In C# use maxWidth = 2 * levels - 1. The outer loop steps i by 2; indent with spaces, then print i squares from a running counter m.
Approach
How to Solve It
One odd-step outer loop with indent, then a continuous square counter.
Method
Idea
Best for
Centered pyramid
Indent, then print m*m with {0,4}
Learning, interviews, exams
Levels input
Same logic with maxWidth = 2*levels - 1
Practice / demos
Pseudocode
Pseudocode
maxWidth = 2 * levels - 1
m = 1
for i from 1 to maxWidth step 2:
for j from i to maxWidth-1:
print two spaces
repeat i times:
print m*m (width 4), then m = m + 1
print newline
Cheat sheet
Goal
Pattern
Odd row widths
for (i = 1; i <= maxWidth; i += 2)
Center the row
for (j = i; j < maxWidth; j++) Console.Write(" ");
Next square
Console.Write("{0,4}", m * m); m++;
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each indent or square
Console.WriteLine
Ends the current line
After both inner loops
Try it
Live Preview
Change the level count and the square pyramid updates instantly.
Whole numbers from 1 to 5 (keeps width-4 columns readable). Tap a chip or type a value — the preview redraws as you go.
maxWidth = 5. Trace indents and the running square counter.
i
Indent / squares
Printed row
1
4 pads / 1
1
3
2 pads / 4 9 16
4 9 16
5
none / 25..81
25 36 49 64 81
Total squares for n levels = n². Counter m never resets between rows.
Code
C# Programs
Three complete programs: fixed 5 levels, user-input levels, and a compact 3-level demo. Use View Output for sample results.
Example 1 — Fixed levels = 5 (maxWidth = 9)
Odd-step outer loop: indent, then print consecutive m*m values with width 4.
C#
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();
}
}
}
}
1. Odd widths.i takes 1, 3, 5, 7, 9 — that many squares print on each row.
2. Indent. Print " " while j runs from i to just below 9 so narrow rows stay centered.
3. Squares. Print m*m with {0,4}, then increment m — never reset between rows.
Example 2 — User Input (levels)
Read the level count and set maxWidth = 2 * levels - 1.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int levels, maxWidth;
int i, j, k, m = 1;
Console.Write("Enter levels: ");
if (!int.TryParse(Console.ReadLine(), out levels) || levels < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
maxWidth = 2 * levels - 1;
for (i = 1; i <= maxWidth; i += 2)
{
for (j = i; j < maxWidth; j++)
Console.Write(" ");
for (k = 1; k <= i; k++)
{
Console.Write("{0,4}", m * m);
m++;
}
Console.WriteLine();
}
}
}
}
2. Same pyramid. Three levels end at 81 with nine squares total.
Example 3 — Compact levels = 3
A smaller fixed demo — same centered idea, easier to trace by hand.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int levels = 3;
int maxWidth = 2 * levels - 1;
int i, j, k, m = 1;
for (i = 1; i <= maxWidth; i += 2)
{
for (j = i; j < maxWidth; j++)
Console.Write(" ");
for (k = 1; k <= i; k++)
{
Console.Write("{0,4}", m * m);
m++;
}
Console.WriteLine();
}
}
}
}
Output
1
4 9 16
25 36 49 64 81
How It Works
1. Same rules. Odd i sets count; indent; print the next squares.
2. Quick check. The base row has five values: 25 through 81.
Edge Cases & Pitfalls
Check these before calling the solution done.
reset m
Reset m = 1 each row
That reprints 1, 4, 9 on every line. Keep m outside the outer loop.
i++
Use i++ instead of i += 2
Row widths become 1, 2, 3… instead of odd lengths. Keep the step of 2.
no format
Print m*m without width
Columns drift once values hit three digits. Prefer Console.Write("{0,4}", m * m).
levels = 1
Single 1
Output is just the formatted 1 — one level, one square.
overflow
m * m overflows int
For large levels, use long m so squares past ~46340 stay correct.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
Total printed squares are 1 + 3 + 5 + … + (2n-1) = n², so work is quadratic in the level count.
Remember
Key Takeaways
Rule: odd row widths via i += 2; each cell prints the next m*m.
Center: indent with " " before printing; use {0,4} for column alignment.
Write vs WriteLine: indents and squares stay on the line; WriteLine advances after each row.
Next step: Program 42 prints a hollow square border of 1s.
One line: for each odd width, indent then print the next perfect squares from a shared counter.
Frequently Asked Questions
A centered pyramid of perfect squares: row 1 prints 1 (1²), row 2 prints 4 9 16 (2², 3², 4²), and so on.
The outer loop increases i by 2 each time (i += 2), so i takes odd values — each becomes the count of squares printed on that row.
An indentation loop prints spaces before each row. As i grows, fewer spaces are printed, so wider rows shift left and stay centered.
m starts at 1 and increments after every printed square. Each value printed is m*m — the next perfect square in sequence.
Fixed-width columns keep the pyramid aligned as squares grow from 1 to 625. Without it, columns drift apart.
Increase levels so maxWidth = 2*levels - 1 grows — see Example 2.
Program 40 alternates 1 and 0 with shrinking rows. Program 41 prints perfect squares in a centered pyramid with growing odd-width rows.
O(n²) for n levels — total prints are 1+3+5+...+(2n-1) = n².
For many levels, m*m overflows int. Switch m to long when squares exceed about 46340.
🤔
Did you know?
Each printed value is m² from a running counter m. Row widths are odd (1, 3, 5, 7, 9) — total prints for n levels = n².