Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid.
public static void main(String[] strings) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of lines (1-15): "); int numberOfLines = input.nextInt(); input.close(); while (numberOfLines < 1 || numberOfLines > 15) { System.out.println("Invalid input"); System.out.print("Enter the number of lines (1-15): "); numberOfLines = input.nextInt(); } for (int currentRow = 1; currentRow <= numberOfLines; currentRow++) { for (int currentColumn = 1; currentColumn <= numberOfLines * currentRow; currentColumn++) { System.out.printf("%3s", " "); } for (int number = currentRow; number >= 1; number--) { System.out.printf("%3d", number); } for (int number = 2; number <= currentRow; number++) { System.out.printf("%3d", number); } System.out.println(); } }
Enter the number of lines (1-15): 15 1 2 1 2 3 2 1 2 3 4 3 2 1 2 3 4 5 4 3 2 1 2 3 4 5 6 5 4 3 2 1 2 3 4 5 6 7 6 5 4 3 2 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15