Skip to content
Advertisement

Invert incrementing triangle pattern

I am writing some code that creates a incrementing number right angle triangle that looks something like this:

JavaScript

I am unsure on how to make my code output a triangle to that shape as my code outputs the same thing except inverted.

This is the code I have:

JavaScript

My speculation is that instead of incrementing some of the values I would decrement them however the code would run infinite garbage values and not what I wanted.

Advertisement

Answer

It is needed to print some spaces before printing the numbers in each row, and the number of spaces should be decreasing depending on the row:

JavaScript

This prefix may be build using String::join + Collections::nCopies:

JavaScript

Or since Java 11, this prefix may be replaced using String::repeat:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement