00:00

Java Program to Print Downward Left Triangle Number Pattern - 3

Last updated:

In this program we print a downward left triangle number pattern. The first row prints the numbers 1 to 5, and each row after it starts one number later and one space further to the right, until only 5 is left.

Like every pattern program, it is built from nested loops: the outer loop picks the row, and the inner loops print what goes on that row.

1. The Pattern

For 5 rows the program prints:

Pattern
12345
 2345
  345
   45
    5

2. How the Program Works

Row number row needs two things: row - 1 spaces to push the numbers to the right, then the numbers from row up to the number of rows.

rowSpacesNumbers printed
101 2 3 4 5
212 3 4 5
323 4 5
434 5
545
  • The outer loop runs once for each row, from 1 to numberOfRows.
  • The first inner loop prints row - 1 spaces.
  • The second inner loop prints the numbers from row to numberOfRows.
  • System.out.println() moves to the next line after each row.

3. Java Program

Pattern12.java
public class Pattern12 {
    public static void main(String[] args) {
        int numberOfRows = 5;

        // outer loop for the rows
        for (int row = 1; row <= numberOfRows; row++) {
            // inner loop to print the spaces
            for (int column = 1; column < row; column++) {
                System.out.print(" ");
            }
            // inner loop to print the numbers
            for (int column = row; column <= numberOfRows; column++) {
                System.out.print(column);
            }
            // move to the next line
            System.out.println();
        }
    }
}
Output
12345
 2345
  345
   45
    5

4. Print the Pattern for Any Number of Rows

To let the user choose the size of the triangle, read the number of rows with a Scanner. The loops stay the same.

Pattern12Input.java
import java.util.Scanner;

public class Pattern12Input {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int numberOfRows = scanner.nextInt();
        scanner.close();

        for (int row = 1; row <= numberOfRows; row++) {
            for (int column = 1; column < row; column++) {
                System.out.print(" ");
            }
            for (int column = row; column <= numberOfRows; column++) {
                System.out.print(column);
            }
            System.out.println();
        }
    }
}
Output (with sample input)
Enter the number of rows: 7
1234567
 234567
  34567
   4567
    567
     67
      7
Note: from 10 rows on, two-digit numbers make the rows wider than the spaces in front of them, so the right edge is no longer straight. Print each number with a fixed width, for example System.out.printf("%3d", column), and print three spaces instead of one to keep it lined up.

5. Summary

  • The outer loop picks the row; the inner loops print the spaces and the numbers for that row.
  • Row row has row - 1 spaces and the numbers from row to numberOfRows.
  • Each row is one number shorter than the one above it, which gives the downward triangle.

Related Programs