1

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

           1
         1 2
       1 2 3
     1 2 3 4
   1 2 3 4 5
 1 2 3 4 5 6

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:

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

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.

Cyrus
  • 49
  • 1
  • 1
  • 7

6 Answers6

2

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:

int rows = 6;

for (int i = 1; i <= rows; ++i) {
    for (int j = rows - i; j >= 1; j--) {
        System.out.print("  ");
    }
    for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
    }
    System.out.println();
}

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

System.out.print(String.join("", Collections.nCopies(rows - i, "  ")));

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

System.out.print("  ".repeat(rows - i));
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
1

Instead of two nested for loops, you can use a single while loop with two incrementing variables. The number of iterations stays the same.

int n = 7, i = 0, j = 0;
while (i < n) {
    // element
    if (i + j >= n - 1) {
        // print an element
        System.out.print(i + j + 2 - n);
    } else {
        // print a whitespace
        System.out.print(" ");
    }
    // suffix
    if (j < n - 1) {
        // print a delimiter
        System.out.print(" ");
        j++;
    } else {
        // print a new line
        System.out.println();
        j = 0;
        i++;
    }
}

Output:

            1
          1 2
        1 2 3
      1 2 3 4
    1 2 3 4 5
  1 2 3 4 5 6
1 2 3 4 5 6 7

See also: Optimized Bubble Sort

1

Your approach is almost correct - use two nested for loops, all that remains is to add one if else statement and calculate the sum of coordinates i and j.

Try it online!

public static void main(String[] args) {
    int n = 6;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            System.out.print(" ");
            int sum = i + j;
            if (sum > n)
                System.out.print(sum - n);
            else
                System.out.print(" ");
        }
        System.out.println();
    }
}

Output:

           1
         1 2
       1 2 3
     1 2 3 4
   1 2 3 4 5
 1 2 3 4 5 6

See also: Printing a squares triangle. How to mirror numbers?

0

You have to print spaces before printing the numbers to make the triangle look inverted, the number of spaces depends on the amount of numbers you skip which are rows-i, so you can loop from i to rows and print space in each iteration.

int rows = 6;
for (int i = 1; i <= rows; ++i) {
    for (int j = i; j < rows; j++) {
        System.out.print("  ");
    }
    for (int j = 1; j <= i; ++j) {
        System.out.print(j + " ");
    }
    System.out.println();
}

Output:

          1 
        1 2 
      1 2 3 
    1 2 3 4 
  1 2 3 4 5 
1 2 3 4 5 6 
Osama A.Rehman
  • 912
  • 1
  • 6
  • 12
0

You can use the property that for the row containing i as the greatest number of the row the number of the spaces can be calculated as 2*(rows-i). You can rewrite your program like below:

public class Main {
    public static void main(String[] args) {
        int rows = 6;
        for (int i = 1; i <= rows; ++i) {
            for (int nspaces = 0; nspaces < 2 * (rows - i); ++nspaces) {
                System.out.print(" ");
            }
            for (int j = i; j > 0; --j) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

Output:

          1 
        2 1 
      3 2 1 
    4 3 2 1 
  5 4 3 2 1 
6 5 4 3 2 1 
Community
  • 1
  • 1
dariosicily
  • 4,239
  • 2
  • 11
  • 17
0

You can print an inverted triangle using two nested for-loops as follows:

// the number of rows and the
// number of elements in each row 
int n = 6;
// iterating over rows with elements
for (int i = 0; i < n; i++) {
    // iterating over elements in a row
    for (int j = 0; j < n; j++) {
        // element
        if (i + j >= n - 1) {
            // print an element
            System.out.print(i + j + 2 - n);
        } else {
            // print a whitespace
            System.out.print(" ");
        }
        // suffix
        if (j < n - 1) {
            // print a delimiter
            System.out.print(" ");
        } else {
            // print a new line
            System.out.println();
        }
    }
}

Output:

          1
        1 2
      1 2 3
    1 2 3 4
  1 2 3 4 5
1 2 3 4 5 6

See also: How to draw a staircase with Java?