Incrementing Start Number Pattern in Java

What You’ll Learn
How to print the number pattern 12345, 2345, 345, 45, 5 in Java using nested for loops.
The key idea is that the inner loop begins at the current outer loop index (j = i), so each row starts one number later than the previous row.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
12345
2345
345
45
5Complete Java Program
Fixed five rows: the outer loop sets the starting number on each row; the inner loop prints from that start up to rows.
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = i; j <= rows; j++) {
System.out.print(j);
}
System.out.println();
}
}
}🧠 How It Works
Set the maximum number
int rows = 5; defines both the height and the maximum number printed on each line.
Outer loop (row start)
for (int i = 1; i <= rows; i++) moves the starting number from 1 to rows. Each increment of i shifts the row left by removing the first number.
Inner loop (print i..rows)
for (int j = i; j <= rows; j++) prints from the row’s starting number i up to rows using System.out.print(j).
New line
System.out.println(); completes a row before moving to the next.
Shifted number pattern
Total numbers printed: n + (n-1) + … + 1 = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user decide the maximum number (and the number of rows) using Scanner:
import java.util.Scanner;
public class Main {
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 = i; j <= rows; j++) {
System.out.print(j);
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Validate input (check
Scanner.hasNextInt()) before usingrows - Add spaces between numbers with
System.out.print(j + " ")for readability - Build each row with
StringBuilderwhen formatting gets complex - Start from a different base number by adjusting what you print inside the loop
- Try the reverse idea: keep start fixed at 1 and decrease the end each row (Program 1)
Avoid
- Forgetting
System.out.println()between rows - Hard-coding
5everywhere instead of usingrows - Printing from
1in the inner loop (that would repeat12345on every row) - Closing
System.inwithScannerif you still need console input elsewhere in the same JVM run
Key Takeaways
The outer loop sets the starting number for each row (i = 1..rows).
The inner loop prints numbers from i to rows, so each row becomes shorter.
Total printed digits follow the triangular number count: n(n+1)/2.
This is the same idea as slicing a sequence—just with loops and console output.
❓ Frequently Asked Questions
j = i. As i increases (1, 2, 3, ...), the first printed number shifts forward, producing 12345, then 2345, then 345, etc.System.out.print(j + " ");. You can also trim the trailing space if needed.rows using Scanner and use it as the loop bound. The last printed line will then contain just rows.Explore More Java Number Patterns!
Continue practicing nested loops with more number patterns.
This pattern prints the same total number of digits as other triangular patterns: n(n+1)/2. Only the starting value changes from row to row.
12 people found this page helpful
