Java Program to Print Sandglass Number Pattern
In this program we print a sandglass (hourglass) of numbers. The top half narrows from 1 2 3 4 5 to a single 5, and the bottom half widens back out.
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
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
2. How the Program Works
Here is what each printed line is made of when numberOfRows is 5:
| Line | Leading spaces | Printed after the spaces |
|---|---|---|
| 1 | 0 | 1 2 3 4 5 |
| 2 | 1 | 2 3 4 5 |
| 3 | 2 | 3 4 5 |
| 4 | 3 | 4 5 |
| 5 | 4 | 5 |
| 6 | 3 | 4 5 |
| 7 | 2 | 3 4 5 |
| 8 | 1 | 2 3 4 5 |
| 9 | 0 | 1 2 3 4 5 |
- Both parts print
row - 1spaces and then the numbers fromrowtonumberOfRows, each followed by a space. - Step 1 counts
rowup from 1 tonumberOfRows, so the rows get shorter. - Step 2 counts
rowdown fromnumberOfRows - 1to 1, so the rows get longer again. It starts one row lower so the middle row is printed only once. System.out.println()moves to the next line after each row.
3. Java Program
public class Pattern21 {
public static void main(String[] args) {
int numberOfRows = 5;
// Step 1: top half, rows get shorter
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();
}
// Step 2: bottom half, rows get longer again, without repeating the middle row
for (int row = numberOfRows - 1; row >= 1; 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
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
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.
import java.util.Scanner;
public class Pattern21Input {
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();
// Step 1: top half, rows get shorter
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();
}
// Step 2: bottom half, rows get longer again, without repeating the middle row
for (int row = numberOfRows - 1; row >= 1; 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: 4 1 2 3 4 2 3 4 3 4 4 3 4 2 3 4 1 2 3 4
Note: once the pattern needs numbers above 9, two-digit numbers take up two positions and the shape is no longer lined up. Print each number with a fixed width, for example
System.out.printf("%3d", value), and print three spaces wherever the program prints 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.
- Work out how many spaces and which numbers each row needs, then write one inner loop for each.
- Keep
numberOfRowsin a variable, so the same program can print the pattern in any size.
Related Programs
Was this tutorial helpful?