Skip to main content

Command Palette

Search for a command to run...

pattern 2

Published
1 min readView as Markdown

This pattern looks like this:

1
12
123
1234
12345

Java Code to Print the Pattern:

 public class Pattern2 {
    public static void main(String[] args) {
        int n = 5; // You can change the value of n to print more or fewer rows

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println(); // Move to the next line after printing each row
        }
    }
}

Explanation:

  • The outer loop (for (int i = 1; i <= n; i++)) controls the number of rows. Here, n is the total number of rows.

  • The inner loop (for (int j = 1; j <= i; j++)) controls the number of columns and prints numbers from 1 to i for each row.

  • System.out.print(j) prints the number without moving to the next line.

  • System.out.println() moves the cursor to the next line after each row is printed.

You can modify n to change the number of rows printed in the pattern

More from this blog

Amit singh's blog

235 posts