0
0
JavaProgramBeginner · 2 min read

Java Program to Print Number Pattern

You can print a number pattern in Java using nested for loops; for example, for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } prints numbers from 1 up to the current line number.
📋

Examples

Inputn = 3
Output1 1 2 1 2 3
Inputn = 5
Output1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
Inputn = 1
Output1
🧠

How to Think About It

To print a number pattern, think of each line as a row where you print numbers starting from 1 up to the line number. Use one loop to go through each line and a nested loop inside it to print the numbers for that line.
📐

Algorithm

1
Get the number of lines to print (n).
2
Start a loop from 1 to n for each line.
3
Inside this loop, start another loop from 1 to the current line number.
4
Print the current number in the inner loop followed by a space.
5
After the inner loop ends, print a new line to move to the next line.
6
Repeat until all lines are printed.
💻

Code

java
public class NumberPattern {
    public static void main(String[] args) {
        int n = 5; // number of lines
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}
Output
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
🔍

Dry Run

Let's trace n=3 through the code

1

Start outer loop i=1

Inner loop runs j=1; prints '1 '

2

Print newline after inner loop

Output so far: 1

3

Outer loop i=2

Inner loop runs j=1, j=2; prints '1 2 '

4

Print newline after inner loop

Output so far: 1 1 2

5

Outer loop i=3

Inner loop runs j=1, j=2, j=3; prints '1 2 3 '

6

Print newline after inner loop

Output so far: 1 1 2 1 2 3

i (line)j (number printed)Output line
111
21 21 2
31 2 31 2 3
💡

Why This Works

Step 1: Outer loop controls lines

The outer for loop runs from 1 to n, controlling how many lines are printed.

Step 2: Inner loop prints numbers

The inner for loop runs from 1 to the current line number i, printing numbers in sequence.

Step 3: New line after each row

After printing numbers for a line, System.out.println() moves the cursor to the next line.

🔄

Alternative Approaches

Using while loops
java
public class NumberPattern {
    public static void main(String[] args) {
        int n = 5;
        int i = 1;
        while (i <= n) {
            int j = 1;
            while (j <= i) {
                System.out.print(j + " ");
                j++;
            }
            System.out.println();
            i++;
        }
    }
}
This uses while loops instead of for loops; functionally the same but slightly longer.
Printing pattern in reverse order
java
public class NumberPattern {
    public static void main(String[] args) {
        int n = 5;
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}
This prints the pattern starting from n numbers down to 1, reversing the pattern.

Complexity: O(n^2) time, O(1) space

Time Complexity

The nested loops cause the program to run in O(n^2) time because for each line i, it prints i numbers.

Space Complexity

The program uses constant extra space O(1) as it only prints output and uses a few variables.

Which Approach is Fastest?

All approaches have similar O(n^2) time; using for loops is more concise and readable than while loops.

ApproachTimeSpaceBest For
Nested for loopsO(n^2)O(1)Simple and readable pattern printing
Nested while loopsO(n^2)O(1)When loop conditions are dynamic or less predictable
Reverse pattern with for loopsO(n^2)O(1)Printing patterns in descending order
💡
Use nested loops where the outer loop controls lines and the inner loop prints numbers per line.
⚠️
Beginners often forget to print a new line after each row, causing all output to appear on one line.