Java Program to Print a Reverse Number Pyramid

1. Introduction

In this tutorial, we'll delve into creating a Java program that prints a reverse number pyramid. Unlike the traditional number pyramid, the reverse number pyramid starts with the widest row of numbers and decreases in width with each ascending row. By taking the number of rows as input from the user, this program offers an excellent opportunity to practice loops, input handling, and pattern generation in Java.

2. Program Steps

1. Import the Scanner class to allow for user input.

2. Prompt the user to enter the number of rows for the reverse number pyramid.

3. Utilize nested loops: the outer loop to manage the rows, the first inner loop for leading spaces, and the second inner loop for decrementing the numbers.

4. Compile and execute the program, then input the number of rows to see the reverse number pyramid.

3. Code Program

import java.util.Scanner; // Import Scanner class

public class ReverseNumberPyramid {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // Create Scanner object for input
        System.out.print("Enter the number of rows: "); // Prompt for user input
        int rows = scanner.nextInt(); // Read number of rows from user

        int startNumber = rows * (rows + 1) / 2; // Calculate the starting number

        for(int i = rows; i >= 1; i--) { // Loop for each row in reverse order
            System.out.format("%" + (rows - i + 1) * 2 + "s", ""); // Print leading spaces for alignment
            for(int j = i; j >= 1; j--) { // Loop to print numbers decrementally
                System.out.print(startNumber + " "); // Print current number
                startNumber--; // Decrement number for next print
            }
            System.out.println(); // Move to the next line after each row
        }
        scanner.close(); // Close the scanner
    }
}

Output:

Enter the number of rows: 5
15 14 13 12 11
      10 9 8 7
        6 5 4
          3 2
            1

Explanation:

1. The program starts by importing the Scanner class to read the number of rows desired for the reverse number pyramid from the user. This input determines how tall the pyramid will be.

2. A calculation is performed to determine the start number, which is the sum of the series from 1 to the number of rows. This ensures that the pyramid begins with the correct number at the base.

3. The outer loop iterates from the total number of rows down to 1, effectively building the pyramid from the bottom up. Inside this loop, a formatted print statement creates the necessary leading spaces to align the pyramid to the right.

4. Within the outer loop, a nested loop counts down from the current row number to 1, printing numbers in descending order. This creates the reverse pyramid effect, with numbers decreasing as the rows ascend.

5. After printing the numbers for a row, a newline character is used to move to the next line, continuing the pattern until the pyramid is complete.

6. Finally, the Scanner object is closed to prevent resource leaks, adhering to good programming practices for handling user input in Java.

Comments