00:00

Java Program to Print Pyramid Number Pattern - 3

Last updated: 3 min read

In this program we print a pyramid in which every row reads the same forwards and backwards: it counts up from 1 to the row number and then back down to 1. Because of this, it is also called a palindrome number pyramid.

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

1. The Pattern

For 5 rows the program prints:

Pattern
        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

2. How the Program Works

Every number is printed with a space after it, so each number takes two characters. To keep the pyramid centred, a row is indented by two spaces for every number it is missing compared with the row below it. Here is what each line is made of when numberOfRows is 5:

LineLeading spacesCount upCount downPrinted
181(none)1
261 211 2 1
341 2 32 11 2 3 2 1
421 2 3 43 2 11 2 3 4 3 2 1
501 2 3 4 54 3 2 11 2 3 4 5 4 3 2 1
  • The outer loop counts row from 1 to numberOfRows.
  • The first inner loop prints two spaces numberOfRows - row times, which pushes shorter rows to the centre.
  • The second inner loop counts up from 1 to row: the left half of the row, including the middle number.
  • The third inner loop counts back down from row - 1 to 1: the right half. Starting at row - 1 stops the middle number being printed twice.
  • Together the two halves print 2 × row - 1 numbers, so each row is two numbers wider than the one above.
  • System.out.println() moves to the next line after each row.

3. Java Program

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

        for (int row = 1; row <= numberOfRows; row++) {
            // leading spaces: two for every number missing from this row
            for (int space = 1; space <= numberOfRows - row; space++) {
                System.out.print("  ");
            }
            // left half: count up from 1 to row
            for (int number = 1; number <= row; number++) {
                System.out.print(number + " ");
            }
            // right half: count back down from row - 1 to 1
            for (int number = row - 1; number >= 1; number--) {
                System.out.print(number + " ");
            }
            // move to the next line
            System.out.println();
        }
    }
}
Output
        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

4. Print the Pattern for Any Number of Rows

To let the user choose the size of the pattern, read the number of rows with a Scanner. The loops stay the same. Keep the input between 1 and 9: from 10 upwards the numbers have two digits, take more room than the two spaces allowed for them, and the pyramid is no longer centred.

Pattern22Input.java
import java.util.Scanner;

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

        for (int row = 1; row <= numberOfRows; row++) {
            // leading spaces: two for every number missing from this row
            for (int space = 1; space <= numberOfRows - row; space++) {
                System.out.print("  ");
            }
            // left half: count up from 1 to row
            for (int number = 1; number <= row; number++) {
                System.out.print(number + " ");
            }
            // right half: count back down from row - 1 to 1
            for (int number = row - 1; number >= 1; number--) {
                System.out.print(number + " ");
            }
            // move to the next line
            System.out.println();
        }
    }
}
Output (with sample input)
Enter the number of rows (1-9): 6
          1
        1 2 1
      1 2 3 2 1
    1 2 3 4 3 2 1
  1 2 3 4 5 4 3 2 1
1 2 3 4 5 6 5 4 3 2 1

5. Summary

  • The outer loop picks the row; three inner loops print the indent, the rising half and the falling half.
  • Start the falling half at row - 1 so the middle number appears once.
  • Indent by two spaces per missing number, because each printed number takes two characters.
  • Keep numberOfRows in a variable, so the same program can print the pattern in any size.

Related Programs