Learn how to print an asterisk based triangle of x rows in the console with Java.

Although probably not any kind of client will ask you to do something like this in the real life with print statements in the console, understanding how the proposed logic to build a triangle with asterisks may be useful to boost your programming logic. Besides, you will probably need to do this someday in the college or university as an asignment of Programming I or introduction to Programming. This can be done easily with the following code:

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

Yeah, absolutely functional! But awfully scalable and what about the opposite orientation? In this brief article, we will explain you easily a way of drawing the famous triangles using loops in Java.

Printing a left oriented triangle

As logic to print a left oriented triangle with asterisks in Java, we will create a loop that will iterate the number of rows that the user wants for the triangle. Inside this loop, another loop will run as well and it will print an asterisk as long as the current iteration index is lower than the parent loop iteration index. Using the print statement allows you to concatenate the text in a single line, so the println method runs only once the inner loop ends:

package com.ourcodeworld.sandbox;

public class Sandbox 
{
    public static void main (String args [])
    {
        int rows = 4;
                
        for (int x = 1; x <= rows; x++)
        {
            for (int i = 1; i <= x; i++)
            {
                System.out.print("* ");
            }
            
            System.out.println("");
        }
    }
}

The execution of the previous snippet will print the following triangle:

* 
* * 
* * * 
* * * *

Printing a right oriented triangle

As logic for the right oriented triangle, we will create a loop indexed by j that will iterate the number of rows that the user wants for the triangle as well. However inside this loop, we will have 2 inner loops indexed by i and k. The first loop i only prints an empty character on the same line as long as the current iteration index of i is lower than the difference of the max number of rows minus the current iteration index of the parent (j). The second loop, will print the asterisk as long as the current iteration index identified by k is lower than the current iteration index of the parent loop, namely j:

package com.ourcodeworld.sandbox;

public class Sandbox 
{
    public static void main (String args [])
    {
        int rows = 4;
        
        for(int j=1;j <= rows;j++){
            for(int i=1;i <= rows-j;i++){
                System.out.print("  ");
            }
            
            for(int k=1;k<=j; k++){
                System.out.print("* ");
            }

            System.out.println();
        }
    }
}

The execution of the previous snippet will print the following triangle:

      * 
    * * 
  * * * 
* * * * 

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors