Incrementing Start Number Pattern in Java

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
12345
2345
345
45
5
1

Complete 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.

Java
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

1

Set the maximum number

int rows = 5; defines both the height and the maximum number printed on each line.

Setup
2

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.

Row control
3

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).

Number printing
4

New line

System.out.println(); completes a row before moving to the next.

Line break
=

Shifted number pattern

Total numbers printed: n + (n-1) + … + 1 = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the maximum number (and the number of rows) using Scanner:

Java
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 using rows
  • Add spaces between numbers with System.out.print(j + " ") for readability
  • Build each row with StringBuilder when 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 5 everywhere instead of using rows
  • Printing from 1 in the inner loop (that would repeat 12345 on every row)
  • Closing System.in with Scanner if you still need console input elsewhere in the same JVM run

Key Takeaways

1

The outer loop sets the starting number for each row (i = 1..rows).

2

The inner loop prints numbers from i to rows, so each row becomes shorter.

3

Total printed digits follow the triangular number count: n(n+1)/2.

4

This is the same idea as slicing a sequence—just with loops and console output.

❓ Frequently Asked Questions

Because the inner loop starts at j = i. As i increases (1, 2, 3, ...), the first printed number shifts forward, producing 12345, then 2345, then 345, etc.
Inside the inner loop, print a space after each number: System.out.print(j + " ");. You can also trim the trailing space if needed.
Yes. Read rows using Scanner and use it as the loop bound. The last printed line will then contain just rows.
O(n²) for n rows: you print n+(n-1)+…+1 = n(n+1)/2 numbers.

Explore More Java Number Patterns!

Continue practicing nested loops with more number patterns.

All Number Patterns →
Did you know?

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.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful