Java Program to Print Pyramid Number Pattern - 3
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:
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:
| Line | Leading spaces | Count up | Count down | Printed |
|---|---|---|---|---|
| 1 | 8 | 1 | (none) | 1 |
| 2 | 6 | 1 2 | 1 | 1 2 1 |
| 3 | 4 | 1 2 3 | 2 1 | 1 2 3 2 1 |
| 4 | 2 | 1 2 3 4 | 3 2 1 | 1 2 3 4 3 2 1 |
| 5 | 0 | 1 2 3 4 5 | 4 3 2 1 | 1 2 3 4 5 4 3 2 1 |
- The outer loop counts
rowfrom 1 tonumberOfRows. - The first inner loop prints two spaces
numberOfRows - rowtimes, 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 - 1to 1: the right half. Starting atrow - 1stops the middle number being printed twice. - Together the two halves print
2 × row - 1numbers, 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
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();
}
}
}
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.
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();
}
}
}
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 - 1so the middle number appears once. - Indent by two spaces per missing number, because each printed number takes two characters.
- Keep
numberOfRowsin a variable, so the same program can print the pattern in any size.