A common problem many new Java developers is to write a program that prints out a triangle. There a lots of variations on this problem but lets start with a simple case and then have a look at some possible variations.

Probably the simplest case is a left aligned triangle of stars that looks like this:

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

To solve this we need to break the problem down. Firstly we identify that we need to print 10 lines, we can use a for loop to achieve this.

for (int i=1; i<=10; i++) { // Print a line of the triangle }

Then we need to work out how to print each line. Each line adds one additional asterisk, in fact the number of stars required is represented by the loop variable i. With this information we can use a nested loop to output the required number of stars.

Here’s the source code for the complete solution to help you understand how it all fits together. Please study the code and let us know if you have any questions.

Once it all makes sense have a look at the discussion of a few variations of this problem below.

package com.learnjava.math.triangle; /** * Write a Java program that prints out a triangle * * * * ** * *** * etc * * @author http://learn-java-by-example.com * */ public class SimpleTriangle { public static void main(String[] args) { // Our triangle will have 10 lines // One loop iteration each line for (int i=1; i<=10; i++) { // For each loop iteration // we want to print a number of stars // 1st line -> 1 star // 2nd line -> 2 star // ... for (int j=0;j<i; j++) { System.out.print("*"); } System.out.println(); } } }

## Turn it upside down

What if we want to turn our triangle upside down?

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

We are still printing the same number of lines, so our outer loop stays the same. What changes is the number of stars that are printed for each line.

Full source code for the Reverse Triangle example is available for download to our members.

## Pyramid

A common variation is to display the triangle as a (symmetric) pyramid.

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

Again the logic is very similar, difference being this time for each line we need to print a number of spaces before printing our stars.

Full source code for the Pyramid example is available for download to our members.

## Numbers instead of Stars/Asterisks

Instead of using stars to print our triangle we may be asked to use numbers. This type of problem generally requires you to implement an additional pattern in the numbers used.

For example using 0s and 1s, alternating what is used to start the line.

0 10 010 1010 01010 101010

Or counting down numbers for each line, and incrementally decreasing what each line starts with.

54321 4321 321 21 1

Once you mastered these we would suggesting looking at the example covering how to generate a Pascals Triangle.

Pascals Triangle | Learn Java by Example[…] If you found this problem a little challenging then we would suggest having a look at some simpler examples of printing triangles using Java. […]