A square number pyramid prints consecutive squares (1, 4, 9, 16, …) in a centered triangle with odd-width rows.
Remember
Rule: maxOdd = 2*rows-1; m = 1
for i = 1, 3, 5, … maxOdd:
print leading spaces, then i values of format("%4d", m*m)
1
4 9 16
25 36 49 64 81 ← rows = 3
Unlike Program 40 (repeated 1/0 digits), here a shared counter m feeds squared values into a centered pyramid.
Approach
How to Solve It
Walk odd widths i = 1, 3, 5, …; indent with spaces, then print i squares via m++.
Method
Idea
Best for
Three loops + format
Odd width, space indent, format("%4d", m*m)
Learning, interviews, exams
Cube variant
Same shape; print m*m*m instead of m*m
When you want cubes in the same pyramid
Pseudocode
Pseudocode
maxOdd = 2 * rows - 1
m = 1
for i from 1 to maxOdd step 2:
for j from i to maxOdd - 1:
print " "
for k from 1 to i:
print m*m (width 4); m = m + 1
print newline
Cheat sheet
Goal
Pattern
Odd widths
for (int i = 1; i <= maxOdd; i += 2)
Leading spaces
for (int j = i; j < maxOdd; j++) print(" ")
Print square
System.out.format("%4d", m * m); m++;
End the row
System.out.println();
Values per row
i (odd: 1, 3, 5, …)
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print / format
Stays on the same line
Spaces and each squared value
System.out.println
Ends the current line
After spaces and numbers finish
Print values without a newline, then end the row once.
Try it
Live Preview
Change the row count and the square pyramid updates instantly.
Whole numbers from 2 to 5 (keeps %4d readable). Tap a chip or type a value — the preview redraws as you go.
Trace odd width i, the m values used, and the printed squares.
i
Count
m used
Squares
Printed row
1
1
1
1
1
3
3
2..4
4, 9, 16
4 9 16
5
5
5..9
25..81
25 36 49 64 81
Total values = 1 + 3 + 5 = 9 = 3². In general, n rows print n² numbers.
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and a cube pyramid. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded size — three nested loops and %4d formatting.
Java
public class SquareNumberPyramid {
public static void main(String[] args) {
int rows = 5;
int maxOdd = 2 * rows - 1;
int m = 1;
for (int i = 1; i <= maxOdd; i += 2) {
for (int j = i; j < maxOdd; j++)
System.out.print(" ");
for (int k = 1; k <= i; k++) {
System.out.format("%4d", m * m);
m++;
}
System.out.println();
}
}
}
2. Indent. The space loop prints " " so shorter rows sit centered.
3. Squares.format("%4d", m*m) prints the next square; m++ continues the sequence.
When i = 1: one value 1. When i = 3: 4, 9, 16.
Example 2 — Rows Input
Read rows at runtime. Same three-loop core.
Java
import java.util.Scanner;
public class SquareNumberPyramidInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
if (rows < 1) return;
int maxOdd = 2 * rows - 1;
int m = 1;
for (int i = 1; i <= maxOdd; i += 2) {
for (int j = i; j < maxOdd; j++)
System.out.print(" ");
for (int k = 1; k <= i; k++) {
System.out.format("%4d", m * m);
m++;
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 5)
Enter the number of rows: 5
1
4 9 16
25 36 49 64 81
100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625
How It Works
1. Prompt and guard. Read rows; exit early if it is less than 1.
2. Same core. Only the source of rows changes from a literal to user input.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — Cube Pyramid
Same centered shape — print m*m*m instead of m*m.
Java
public class SquareNumberPyramidCubes {
public static void main(String[] args) {
int rows = 3;
int maxOdd = 2 * rows - 1;
int m = 1;
for (int i = 1; i <= maxOdd; i += 2) {
for (int j = i; j < maxOdd; j++)
System.out.print(" ");
for (int k = 1; k <= i; k++) {
System.out.format("%4d", m * m * m);
m++;
}
System.out.println();
}
}
}
Output
1
8 27 64
125 216 343 512 729
How It Works
1. Same structure. Odd widths and space indent are unchanged.
2. Cubes. Only the printed expression becomes m * m * m.
3. Width tip. Use %5d or wider when cubes grow past three digits.
Edge Cases & Pitfalls
Check these before calling the solution done.
println inside
Column of numbers
If println is inside the number loop, each square lands on its own line. Use format for values; println only after the row.
Reset m
Restarted squares
Declaring m = 1 inside the outer loop restarts the sequence every row instead of continuing.
No spaces
Left-aligned triangle
Skipping the space loop keeps the odd-width shape but loses the centered pyramid look.
No %4d
Crowded columns
Plain print(m*m) makes multi-digit squares crowd earlier columns.
rows = 1
Single value
Output is just 1 on one line.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Square / cube pyramid (Examples 1–3)
O(n²)
O(1)
Total values = 1 + 3 + … + (2n−1) = n². Only loop counters are stored.
Remember
Key Takeaways
Rule: odd width i; print i values of m*m with %4d, then m++.
Keep m outside: one shared counter continues the square sequence across rows.
Center first: print leading " " spaces before the numbers on each row.
Complexity:O(n²) time; O(1) extra space.
One line: for each odd width, indent with spaces, print i squares via format("%4d", m*m), then println().
Frequently Asked Questions
Odd widths (1, 3, 5, …) keep the pyramid symmetric. The last row has 2*rows-1 numbers.
A counter m starts at 1 and increments after every print. The program outputs m*m, producing 1, 4, 9, 16, 25, and so on.
Before printing numbers, a space loop runs from j=i to maxOdd-1, printing leading pairs of spaces so smaller rows shift right.
Fixed-width columns keep alignment when values grow to three digits (121) or more.
Yes. Replace m*m with m*m*m — see Example 3.
O(n²) for n rows because total printed numbers are 1+3+5+…+(2n-1) = n².
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
maxOdd becomes negative or zero and the outer loop never runs. Validate and prompt again for interactive programs.
🤔
Did you know?
Each row prints an odd count of squared values (1, 3, 5, …). A counter m increments after every print and the program outputs m*m with fixed-width formatting — total numbers equal n² for n rows.