An increasing suffix pattern starts with a single digit and grows by one digit on the left each row — until the last row shows 1..n.
Remember
Rule: for i = n down to 1
print j from i to n
5
45
345
2345
12345 ← n = 5
Unlike Program 5 (1, 12, 123), here the outer loop counts down and each row ends at n.
Approach
How to Solve It
Count the start value down from n; print every digit from that start through n.
Method
Idea
Best for
Fixed n
Outer i = n..1; inner j = i..n
Labs and demos
Scanner input
Same loops; read n at runtime
Interactive practice
Spaced digits
print(j + " ")
Easier reading
Pseudocode
Pseudocode
for i from n down to 1:
for j from i to n:
print j
new line
Cheat sheet
Goal
Pattern
Set size
int n = 5;
Outer loop
for (int i = n; i >= 1; i--)
Inner loop
for (int j = i; j <= n; j++)
Print digit
System.out.print(j);
End row
System.out.println();
Digits on row i
n - i + 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit in the row
System.out.println
Ends the line
After the inner loop finishes
Build the row with print, then break once.
Try it
Live Preview
Change n and the increasing suffix pattern updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · digits = 15
5
45
345
2345
12345
Trace
Worked Walkthrough — n = 3
Trace each outer value of i and the digits printed on that row.
i
Inner j
Printed row
3
3
3
2
2 3
23
1
1 2 3
123
Each step lowers the start of the range by 1, so one more digit appears on the left.
Code
Java Programs
Three complete programs: fixed n = 5, Scanner input, and spaced digits. Use View Output to reveal sample results.
Example 1 — Fixed n = 5
Hard-coded height — outer loop counts down; each row prints i..n.
Java
public class IncreasingSuffixPattern {
public static void main(String[] args) {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
Output
5
45
345
2345
12345
How It Works
1. Outer loop.i starts at 5 and counts down to 1.
2. Inner loop. For each i, print j from i to n — first row is 5, then 45, and so on.
3. Newline.println() after the inner loop starts the next row.
Example 2 — Scanner Input
Read n at runtime. Same nested-loop core.
Java
import java.util.Scanner;
public class IncreasingSuffixPatternInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int n = sc.nextInt();
if (n < 1) return;
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
System.out.print(j);
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter the number of rows: 4
4
34
234
1234
How It Works
1. Prompt and guard. Read n; exit early if it is less than 1.
2. Same loops. Only the source of n changes — outer down, inner i..n.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int n = sc.nextInt();
if (n < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — Spaced Digits
Same loops; print a space after each digit for easier reading.
Java
public class IncreasingSuffixPatternSpaced {
public static void main(String[] args) {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = i; j <= n; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
Output
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
How It Works
1. Same shape. Outer and inner bounds match Examples 1 and 2.
2. Formatting only.j + " " separates digits — useful when numbers grow past single digits.
3. Trailing space. Each row may end with a space; that is normal for this simple style.
Edge Cases & Pitfalls
Check these before calling the solution done.
Outer ascending
Wrong row order
Counting i up from 1 prints 12345 first — that is Program 2, not this pattern.
j from 1
Wrong shape
Starting j at 1 every row builds an ascending triangle (Program 5 style), not a growing suffix.
println inside
Vertical digits
Calling println inside the inner loop prints one digit per line. Keep it after the inner loop.
n = 1
Single digit
Output is just 1 on one line.
n ≤ 0
Empty output
The outer loop never runs. Guard interactive input with n >= 1.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(1)
Total digits printed: 1 + 2 + … + n = n(n+1)/2 → still O(n²).
Remember
Key Takeaways
Rule: outer i = n..1; print j = i..n on each row.
Growth: one new digit appears on the left each row.
Vs Program 2: same inner loop; flip the outer direction to reverse row order.
Complexity:O(n²) for n rows.
One line: start at n and walk the start value down — each row prints from that start through n.
Frequently Asked Questions
On the first iteration i equals n, so the inner loop prints only n (just 5). Each next row starts one value earlier, so the line grows on the left.
The outer loop runs i from n down to 1. For each row i, the inner loop runs j from i to n and prints j, then println ends the row.
Program 2 prints 12345, then 2345, then 345 (outer loop counts up). Program 6 prints 5, then 45, then 345 (outer loop counts down) — same inner loop, reversed row order.
Program 5 prints 1, 12, 123 (inner loop j = 1..i). Program 6 prints 5, 45, 345 (inner loop j = i..n with a descending outer loop).
Yes. Use System.out.print(j + " ") inside the inner loop — see Example 3.
O(n²) for n rows. Total printed digits are 1+2+…+n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
The outer loop starts at n and counts down — each row prints digits from i through n, so the suffix grows on the left: 5, 45, 345, …