Shape Rule
Growing rows
Row i prints numbers 1..i; leading spaces right-align the triangle.

The right-aligned increasing number triangle prints 1, then 1 2, then 1 2 3, and so on — with leading spaces so each row aligns on the right. This tutorial covers the space loop, number loop, live preview, algorithm steps, worked Java examples, edge cases, and complexity.
Growing rows
Row i prints numbers 1..i; leading spaces right-align the triangle.
j = rows..i+1
for (j = rows; j > i; j--) prints rows - i leading spaces.
1..i per row
System.out.format("%2d", k) prints k = 1..i on every row.
Row index i
for (i = 1; i <= rows; i++) walks each row of the triangle.
1–20 rows
Pick a row count and draw the right-aligned increasing number triangle instantly in the browser.
Complexity
Prints n(n+1)/2 numbers across n rows — n² iterations; extra memory stays O(1).
A right-aligned increasing number triangle prints 1, then 1 2, then 1 2 3, up to rows. With rows = 5, the widest row sits flush right and shorter rows indent from the left.
In Java you use an outer loop for each row, a space loop that prints rows - i spaces, and a number loop that prints System.out.format("%2d", k) for k = 1..i.
It teaches indent-then-content alignment — the same trick used for pyramids, diamonds, and right-aligned star patterns.
Space loop first, then number loop — both run on every row.
rows - i spaces shrink each row toward the right edge.
Two-character fields keep columns aligned when numbers reach two digits.
Follow Program 42 hollow square; continue to Program 44 number diamond.
In short: for each row i, print rows - i spaces, then print numbers 1..i with %2d, then call System.out.println().
Given a positive integer rows (e.g. 5), print a right-aligned triangle: row i shows numbers 1..i with leading spaces.
// rows = 5 (conceptual shape)
// 1
// 1 2
// 1 2 3
// 1 2 3 4
// 1 2 3 4 5 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle rows to print (typically ≥ 1). |
| Printed output | text | Right-aligned rows of increasing numbers; row i has i values. |
for i from 1 to rows:
print (rows - i) spaces
for k from 1 to i: print k with %2d
print newline | Approach | Idea | Best for |
|---|---|---|
| Space + number loops | Leading spaces, then 1..i with %2d | Learning and interviews |
| User-input size | sc.nextInt(); | Flexible console programs |
| Left-aligned triangle | Skip the space loop | Contrast with right alignment |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = 1; i <= rows; i++) |
| Print spaces | for (j = rows; j > i; j--) |
| Print numbers | for (k = 1; k <= i; k++) + %2d |
| End the row | System.out.println(); |
| Program 42 contrast | Hollow square uses a border if; this pattern uses spaces + increasing numbers |
Same triangle row — how leading spaces and numbers work together.
j = rows; j > i; j--Prints rows - i leading spaces
k = 1..iPrints System.out.format("%2d", k)
i numbersRow i always prints i numbers
trace i=3Dry-run row 3: two spaces then 1 2 3
Reach for this pattern when teaching indent-then-content alignment with nested loops.
Classic follow-up after hollow squares and centered pyramids.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible row count.
Compare with Program 42 (hollow square), then continue to Program 44 (number diamond).
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 row count between 1 and 20 and draw the right-aligned increasing number triangle in the browser.
Three complete Java programs — fixed row count, Scanner input, and a left-aligned variant. Click View Output to reveal sample console results.
Print five rows with a space loop and number loop per row.
rows = 5Hard-coded size — nested loops and a space loop build each row.
public class RightAlignedNumberTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.format("%2d", k);
}
System.out.println();
}
}
} When i = 1, the space loop prints four spaces, then one formatted 1. When i = 3, two spaces precede 1 2 3 — the triangle grows downward and right.
Let the user choose the height at runtime.
Read the row count with Scanner.nextInt() (check hasNextInt() in real apps).
import java.util.Scanner;
public class RightAlignedNumberTriangleInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.format("%2d", k);
}
System.out.println();
}
sc.close();
}
} Same nested-loop core as Example 1; only the source of rows changes. Non-numeric input throws InputMismatchException with nextInt() — check hasNextInt() for safer labs.
Remove the space loop to print a completely left-aligned triangle.
Skip the space loop so numbers start at the left margin — a common contrast exercise.
public class LeftAlignedNumberTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int k = 1; k <= i; k++) {
System.out.format("%2d", k);
}
System.out.println();
}
}
} Same number loop; removing the leading-space loop leaves rows flush left.
System.out is built in; use Scanner when reading input. Set rows (fixed or from input).
for (i = 1; i <= rows; i++) — walks each row of the triangle.
for (j = rows; j > i; j--) prints rows - i leading spaces.
System.out.format("%2d", k) for k = 1..i, then println() ends the row.
Total numbers printed: n(n+1)/2 — O(n²) time, O(1) extra memory.
rows = 5, row i = 3Trace one middle row to see spaces and numbers combine.
| Step | Loop | Prints |
|---|---|---|
| Spaces | j = 5, 4 (2 times) | |
| Numbers | k = 1 | 1 |
| Numbers | k = 2 | 2 |
| Numbers | k = 3 | 3 |
Row output: 1 2 3 — total numbers for full triangle: 1+2+3+4+5 = 15 = n(n+1)/2.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: remove the space loop and watch rows snap left.
Foundation for number diamonds, centered pyramids, and mirrored patterns.
Example: mirror rows downward to build Program 44’s diamond.
Practice System.out.print vs row newline without complex math.
Example: put System.out.println() inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print 1, 12, 123 without spaces between digits.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for n = 5 → 15.
Pair the pattern with Scanner 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 Java courses.
Wrong bounds show up immediately as a misaligned or left-aligned shape.
Only loops and console output — no arrays or math libraries.
Invert, center, left-align, or swap digits for stars with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the space-loop version first; then try the left-aligned triangle in Example 3.
Small habits that keep number-pattern code clean.
Use rows (or n) and keep i/j for row/column — or rename to row/col.
ScannerAvoid crashes when the user types letters instead of a number.
Only call System.out.println() after the inner loop finishes the row.
One-liner for spaces: System.out.print(" ".repeat(rows - i)); before the number loop.
Trace rows = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of digits per line, you almost certainly put System.out.println() inside the inner loop.
Mistakes that commonly break right-aligned number triangles.
Each number lands on its own line — you get a column, not a triangle.
→ Use System.out.print for spaces and numbers; System.out.println() only after both inner loops.
Using j = 1..i for spaces under-indents rows and breaks right alignment.
→ Keep for (j = rows; j > i; j--) to print rows-i leading spaces.
Omitting System.out.println() glues every row onto one endless line.
→ Always end the row after the inner loop.
Letters or empty input throw InputMismatchException.
→ Prefer Scanner and re-prompt on failure.
Printing bare k without %2d breaks alignment once values reach two digits.
→ Use System.out.format("%2d", k) so columns stay aligned.
Check these inputs before calling the solution done.
Output is one indented 1 when right-aligned.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Output grows as n(n+1)/2 numbers plus spaces — fine for labs, noisy for huge n.
Unchecked Scanner leaves rows unset — call sc.hasNextInt() first.
Remove the space loop — see Example 3.
Try these variations to lock in the pattern.
i from rows down to 11, 12, 123 without spacesn(n+1)/2; leading spaces add roughly the same order of characters.print stays on the line; println advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print one indented 1.System.out.format("%2d", k) so columns stay aligned as values grow.Quick Takeaway: space loop for alignment, number loop for 1..i, %2d formatting, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(rows²) | O(1) |
| Left-aligned triangle (Example 3) | O(rows²) | O(1) |
The right-aligned increasing number triangle combines nested loops with a simple alignment pattern — a natural step after hollow squares and pyramids. Master the fixed-rows version first, then try user input and the left-aligned triangle.
Practice the three examples above, then continue to Program 44 for the number diamond pattern.
Every row prints i numbers — keep println() only after both inner loops finish.
%2d and println() after each rowrows ≥ 1 for interactive programsScanner return value before using rowsSystem.out.println() inside the inner digit loop%2d needs tworows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints 1..i
Definitionrows - i spaces
CodePrint 1..i with %2d
Logicn(n+1)/2 numbers
I/OO(n²) time
AnalysisEach row prints numbers 1..i and leading spaces push the row to the right — fewer spaces as i grows, which creates the right-aligned triangle.
Move on to the centered number diamond pattern in the Java number-pattern series.
12 people found this page helpful