Java Program for Pattern

Java Program for Pattern
Reading Time: 4 minutes


The Java program is designed to print a right-angled triangle made of stars. The number of rows in the triangle is adjustable, allowing users to control the size of the pattern.


  1. Class Definition:

The program begins with the definition of a class named StarPattern.

  1. Main Method:

The main method is the entry point of the program.

  1. Variable Declaration:

The variable rows is initialized with the value 5, representing the number of rows in the triangle. Users can modify this value to change the size of the pattern.

  1. Outer Loop (Row Iteration):

The outer loop iterates over each row of the triangle. It starts from the first row (i = 0) and continues until the specified number of rows.

  1. Inner Loop (Star Printing):

The inner loop is responsible for printing the stars in each row. The loop runs from 0 to the current row index (i) and prints a star followed by a space for each iteration.

  1. Newline After Each Row:

After printing the stars in a row, a newline character is added to move to the next line, forming the triangle pattern.


The program uses nested loops to control the row and column (star) printing. The outer loop manages the rows, and the inner loop handles the stars in each row. The number of iterations in the inner loop is determined by the current row index, ensuring that the correct number of stars is printed in each row.

Users can customize the size of the triangle by adjusting the rows variable in the code. The result is a simple and adjustable right-angled triangle pattern made of stars.

Code :

     public class StarPattern {
    public static void main(String[] args) {
        int rows = 5; // You can change this value to adjust the size of the triangle
          for (int i = 0; i < rows; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("* ");


Output :

* * 
* * * 
* * * * 
* * * * * 

Copy and paste this code into a Java file (e.g., and run it to see the output. You can modify the rows variable to control the size of the triangle. Let me know if you want a different pattern


Q1: How can I create a Java program for pattern printing?

A1: To craft a Java program for pattern printing, utilize nested loops for precise control over rows and columns.

Q2: What’s the key to mastering Java program star patterns?

A2: Achieving proficiency in Java program star patterns involves understanding loop structures to manipulate star placements effectively.

Q3: Can you guide me on writing a Java program to print a specific pattern?

A3: Certainly! You can design a Java program to print a pattern by strategically arranging loops for the desired pattern structure.

Q4: What’s the secret sauce for a top-notch Java program for pattern printing?

A4: The secret lies in meticulous planning of loop iterations and conditions in your Java program for pattern printing.

Q5: How do I enhance my Java program for pattern printing skills?

A5: Continuous practice and experimentation with loop variations will undoubtedly boost your Java program pattern printing skills over time.