Java Incremental Number Triangle Pattern (Right-Aligned)
Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned incremental triangle prints a continuous counter 1, 2, 3, … across rows, padding the left with spaces and using fixed-width columns so values stay aligned.
Remember
Rule: k = 1; for i from 1 to rows,
for j from rows down to 1:
if j > i: print 3 spaces
else: printf("%3d", k++)
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15 ← rows = 5
Unlike Program 34 (per-cell formula i + j) and Program 30 (descending digits), here one counter k runs across the whole triangle.
Approach
How to Solve It
Grow i from 1 to rows; in a fixed-width descending loop, print spaces while j > i, otherwise print k++ with %3d.
Method
Idea
Best for
Counter + printf
k++ with %3d; spaces while j > i
Learning, interviews, exams
User-input rows
Same logic; read height with Scanner
When triangle size must vary
Pseudocode
Pseudocode
k = 1
for i from 1 to rows:
for j from rows down to 1:
if j > i: print " "
else: print k (width 3); k = k + 1
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (int i = 1; i <= rows; i++)
Fixed-width column
for (int j = rows; j >= 1; j--)
Leading spaces
if (j > i) System.out.print(" ");
Print counter
System.out.printf("%3d", k++);
End the row
System.out.println();
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print / printf
Stays on the same line
Spaces and each formatted number
System.out.println
Ends the current line
After the inner loop finishes
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned incremental triangle updates instantly.
Whole numbers from 3 to 7. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 values
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Trace
Worked Walkthrough — rows = 5
Trace leading space groups (rows − i), numbers taken from k, and the printed row.
i
Spaces
Numbers from k
Printed row
1
4 × 3
1
1
2
3 × 3
2, 3
2 3
3
2 × 3
4, 5, 6
4 5 6
4
1 × 3
7 … 10
7 8 9 10
5
0
11 … 15
11 12 13 14 15
Total numbers = 1 + 2 + … + 5 = 15. Space groups shrink by one each row.
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and a compact dry-run. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded size — continuous k with %3d and three-space padding.
Java
public class RightAlignedIncremental {
public static void main(String[] args) {
int k = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= 1; j--) {
if (j > i)
System.out.print(" ");
else
System.out.printf("%3d", k++);
}
System.out.println();
}
}
}
Output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
How It Works
1. Counter outside.k starts at 1 before the outer loop and never resets.
2. Fixed-width inner loop.j runs from 5 down to 1 — spaces while j > i.
3. Formatted print.printf("%3d", k++) prints the next value in a 3-column field.
When i = 1: four space groups then 1. When i = 5: no spaces — 11 … 15.
Example 2 — Rows Input
Read rows at runtime. Same counter and formatting; inner loop uses rows as width.
Java
import java.util.Scanner;
public class RightAlignedIncrementalInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = sc.nextInt();
if (rows < 1) return;
int k = 1;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= 1; j--) {
if (j > i)
System.out.print(" ");
else
System.out.printf("%3d", k++);
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 3)
Enter rows: 3
1
2 3
4 5 6
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 — Compact rows = 3
Same counter and spacing as Example 1 — smaller size for paper tracing.
Java
public class RightAlignedIncrementalSmall {
public static void main(String[] args) {
int rows = 3;
int k = 1;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= 1; j--) {
if (j > i)
System.out.print(" ");
else
System.out.printf("%3d", k++);
}
System.out.println();
}
}
}
Output
1
2 3
4 5 6
How It Works
1. Same structure. Continuous k and three-space padding — only rows changes from 5 to 3.
2. Spaces shrink. Row 1 has 2 space groups; row 2 has 1; row 3 has none.
3. Dry-run first. Trace i = 1..3 on paper before coding the full rows = 5 demo.
Edge Cases & Pitfalls
Check these before calling the solution done.
println inside
Column of numbers
If println is inside the inner loop, each value lands on its own line. Use print/printf for cells; println only after the inner loop.
Reset k
Restarted sequence
Declaring k = 1 inside the outer loop restarts the count every row instead of continuing.
Wrong space width
Drifted columns
Single spaces instead of " " break alignment with %3d.
No %3d
Crowded digits
Plain print(k++) makes two-digit values crowd earlier columns.
rows = 1
Single value
Output is just 1 (with no leading spaces).
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Right-aligned incremental (Examples 1–3)
O(n²)
O(1)
Total numbers = n(n+1)/2; each row also walks a fixed-width loop of size n — still quadratic. Only counters are stored.
Remember
Key Takeaways
Rule: continuous k++ across rows; spaces while j > i; print with %3d.
Keep k outside: declare k = 1 before the outer loop so the sequence never resets.
Break the row: call println only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: spaces if j > i, else printf("%3d", k++), then println().
Frequently Asked Questions
Numbers keep increasing across rows without resetting — row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on.
Before printing numbers on each row, the program prints three spaces while j > i. This indents the left side so numbers shift right.
The format specifier reserves 3 columns per number (right-aligned), keeping columns aligned when values become two digits.
k is declared outside the loops and increments with k++ each time a number prints, so the sequence continues across rows.
Program 30 prints descending digits per row. Program 35 uses a continuous counter k with fixed-width formatting.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.
🤔
Did you know?
A counter k starts at 1 and increments every time a number is printed. Leading spaces appear while j > i, and System.out.printf("%3d", k++) keeps columns aligned as values grow past single digits.