logo
BeqxCode
  • Home
  • Blog
  • Playground
  • Explore
  • About

BeqxCode

Transforming the future through tech.

hello@beqxcode.com
+254 (113) 334-775
Nairobi, Kenya

Quick Links

  • Home
  • Blog
  • Playground
  • Explore
  • About

Categories

  • Miscellaneous
  • Machine Learning
  • Artificial Intelligence
  • Web development

Stay Updated

Subscribe to my newsletter for updates and announcements.

© 2025 www.beqxcode.com. All rights reserved.
Miscellaneous

Printing patterns in Java - Simple to Advanced + some Trick

Emmanuel Yegon profile picture
Emmanuel Yegon
March 4, 2025
3 min read
Printing patterns in Java - Simple to Advanced + some Trick

Table of Contents

  • A trick behind pattern printing
  • Square pattern
  • Triangle pattern
  • Right half pyramid
  • Reverse right half pyramid
  • Left half pyramid pattern
  • Reverse left half pyramid pattern
  • Hollow triangle pattern
  • Hollow reverse triangle pattern
  • K pattern
Table of Contents
  • A trick behind pattern printing
  • Square pattern
  • Triangle pattern
  • Right half pyramid
  • Reverse right half pyramid
  • Left half pyramid pattern
  • Reverse left half pyramid pattern
  • Hollow triangle pattern
  • Hollow reverse triangle pattern
  • K pattern

In order to enhance your logical skills and the idea of loops, you ought to explore pattern printing in java. From simple to advanced patterns, all have their logically defined ways.

A trick behind pattern printing

Before we venture into printing several patterns in java, let us first explore one trick in developing the logic for a certain pattern.

Let's say you have been given this kind of a pattern, and told to provide its logic in java.

The pattern is a simple right-angled triangle. Now, let's see how we can derive the logic step-wise:

Step 1:

Draw horizontal and vertical lines to get a grid-like box layout as shown:

Now we have 4 rows and columns, each row and column at a certain index i and j respectively.

Step 2:

  • Assume we have a variable named n, which is the required number of rows, e.g. n=4
  • Let us represent the Line(row), with blue and the Star with green.
  • The Line can be represented in either increasing(1,2,3,4,5) or decreasing order(5,4,3,2,1)
  • Closely monitoring the pattern, you can notice that the first Line prints 1 star, second is 2 stars, third is 3 stars, and so on. We have made the green area have this kind of configuration: 1 to 1, 1 to 2 and so on. e.g. 1 to 2 means 2 stars will be printed at the second row and so on.
  • You can simply match the similar patterns, as highlighted in orange.
  • We have variable i for the outer loop and j for the inner loop. Variable i should be less than or equal to n(number of rows) while j should be less than or equal to i.
  • We use a for loop for this case. Variable i has its for loop implemented in this way: for (int i=1; i<=n; i++){...} while for j, for (int j=1; j<=i; j++){...} .

Writing the code will be much easier since we have the complete logic. Finally, let's write the code in Java to match the pattern's logic.

public class rightAngledTriangle{
  // main method
  public static void main(String[] args){
    // initialize no. of rows/lines
    int n = 4;
    // outer loop for the change in row/line
    for (int i = 1; i <= n; i++){
      // inner loop for printing the stars
      for (int j = 1; j <= i; j++){
        // print the stars without change in line
        System.out.print("*"); 
      }
      // change the row/line
      System.out.println();
    }
  }
}

Output:

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

With that in mind, we can start writing code for different types of patterns all the way from the simplest to more advanced. There are several tricks for each pattern and you can take your time to research and explore.

Let us start with the simple patterns:

Square pattern

public class Patterns{
    public static void main(String[] args){
      int n = 8;
      for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            System.out.print("* ");
        }
        System.out.println();
      }
    }
}

Output:

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

Triangle pattern

public class Patterns{
    public static void main(String[] args){
        int n = 8;
        // outer loop handles rows
        for (int i = 0; i < n; i++) {
            // inner loop to print spaces.
            for (int j = n - i; j > 1; j--) {
                System.out.print(" ");
            }

            // inner loop to print stars.
            for (int j = 0; j <= i; j++) {
                System.out.print("* ");
            }

            // printing new line for each row
            System.out.println();
        }
    }
}

Output:

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

Right half pyramid

public class Patterns{
    public static void main(String[] args){
        int n = 8;
        // outer loop to handle rows
        for (int i = 1; i <= n; i++) {

            // inner loop to handle columns
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // print new line for each row
            System.out.println();
        }    
    }
}

Output:

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

Reverse right half pyramid

public class Patterns{
    public static void main(String[] args){
        int n = 8;
        // outer loop to handle rows
        for (int i = n; i >= 1; i--) {

            // inner loop to handle columns
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // print new line for each row
            System.out.println();
        }    
    }
}

Output:

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

Left half pyramid pattern

public class Patterns{
    public static void main(String[] args){
        int n = 8;
         // outer loop to handle rows
        for (int i = n; i >= 1; i--) {

            // inner loop to print spaces.
            for (int j = 1; j < i; j++) {
                System.out.print(" ");
            }

            // inner loop to print stars.
            for (int j = 0; j <= n - i; j++) {
                System.out.print("*");
            }

            // print new line for each row
            System.out.println();
        }   
    }
}

Output:

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

Reverse left half pyramid pattern

public class Patterns{
    public static void main(String[] args){
        int n = 8;
         // calculating number of spaces
        int space = 2 * n - 2;

        // outer loop to handle rows
        for (int i = n; i > 0; i--) {
            // inner loop to print spaces.
            for (int j = 0; j < n - i; j++) {
                System.out.print(" ");
            }
            // Decrement value of num after each loop
            space -= 2;
            // inner loop to print stars.
            for (int j = 0; j < i; j++) {
                System.out.print("*");
            }

            // print new line for each row
            System.out.println();
        } 
    }
}

Output:

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

Hollow triangle pattern

public class Patterns{
    public static void main(String[] args){
        int n = 8;
        // outer loop to handle rows
        for (int i = 1; i <= n; i++) {
          
            // inner loop to print spaces.
            for (int j = i; j < n; j++) {
                System.out.print(" ");
            }
          
            for (int k = 1; k <= (2 * i - 1); k++) {
                // print stars.
                if (k == 1 || i == n || k == (2 * i - 1)) {
                    System.out.print("*");
                }
                // print spaces
                else {
                    System.out.print(" ");
                }
            }

            System.out.println("");
        }
    }
}

Output:

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

Hollow reverse triangle pattern

public class Patterns{
    public static void main(String[] args){
        int n = 8;
         // outer loop to handle rows
        for (int i = n; i >= 1; i--) {

            // inner loop to print spaces.
            for (int j = i; j < n; j++) {
                System.out.print(" ");
            }

            for (int k = 1; k <= (2 * i - 1); k++) {
                // print stars.
                if (k == 1 || i == n || k == (2 * i - 1)) {
                    System.out.print("*");
                }
                // print spaces.
                else {
                    System.out.print(" ");
                }
            }

            System.out.println("");
        }
    }
}

Output:

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

K pattern

public class Patterns{
    public static void main(String[] args){
        int n = 8;
        // upper triangle
        // outer loop to handle rows
        for (int i = n; i >= 1; i--) {

            // inner loop to handle columns
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // printing new line for each row
            System.out.println();
        }

        // lower triangle
        // outer loop to handle rows
        for (int i = 2; i <= n; i++) {

            // inner loop to handle columns
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // printing new line for each row
            System.out.println();
        }
    }
}

Output:

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

More patterns coming up! ✌️

Emmanuel Yegon profile photo

About Emmanuel Yegon

Tech enthusiast, creative developer and writer.

More articles

You might also like

Popular Coding Interview Questions in Java
March 13, 2025

Popular Coding Interview Questions in Java

Develop a Banking System in C
March 9, 2025

Develop a Banking System in C

Develop a phone billing system in java using javax swing library
March 4, 2025

Develop a phone billing system in java using javax swing library