Wrote a code to output a triangle:

int n = key.nextInt(); for (int i = 0; i <= n; i++) { for (int j = i; j > 0; j--) { System.out.print("* "); } System.out.println(); } 

Result:

 * * * * * * * * * * 

How to display this triangle in the reverse order:

  * ** *** **** 

    2 answers 2

    you just need to add a few spaces.

     int n = key.nextInt(); for (int i = 0; i <= n; i++) { for (int j = 0; j < ni; j++) { System.out.print(" "); } for (int j = i; j > 0; j--) { System.out.print("* "); } System.out.println(); } 

      You can do this:

       String text = "*"; for (int i = 1; i < 10; i++) System.out .printf( "%1$10s\n", String.join("", Collections.nCopies(i, text))); 

      Pattern %1$10s\n denotes left alignment with spaces for text of maximum length 10.