Inverted Pyramid & Reverse Number Patterns in Java

⚡ Direct Answer & Key Takeaway

Inverted pyramid and reverse number patterns reverse vertical growth by initializing the outer loop i at n and decrementing with i-- until i >= 1. For a centered inverted star pyramid, print n - i leading spaces followed by 2 * i - 1 asterisks per row. For a reverse number triangle (5 4 3 2 1 down to 1), iterate the inner loop j from i down to 1 inside the same decrementing outer loop.

1. Decrementing Outer Loops for Top-Heavy Geometry

Upward triangles start small and grow downward, whereas inverted pyramids place their widest row at the top and taper to a single point at the bottom. Rather than inventing complicated subtraction formulas inside an ascending outer loop, the cleanest Java pattern sets the outer loop counter i to n and decrements it down to 1 using i--. Because i starts large and shrinks by 1 per row, the familiar star formula 2 * i - 1 automatically yields the widest odd count at the top and narrows down to 1 on the last line. At the same time, the leading space expression n - i starts at 0 on the top row and grows by 1 on each subsequent row, keeping every shortened line centered beneath the top edge.

  • Countdown outer loop header for (int i = n; i >= 1; i--) starts at maximum width.
  • Leading spaces n - i begin at 0 on row 1 and increase by 1 every line downward.
  • Check that the continuation operator is >= (greater than or equal to) whenever the update step is i--.

2. Dry-Run Trace of Spaces and Reverse Values

Trace both blocks cleanly for n = 4. In the Inverted Star Pyramid, when i = 4, s <= 4 - 4 (0) prints 0 spaces, and j runs 1 to 7 printing 7 stars ("*******"). When i = 3, s prints 1 space and j runs 1 to 5 (" *****"). At i = 2, s prints 2 spaces and j prints 3 stars (" ***"). At i = 1, s prints 3 spaces and j prints 1 star (" *"), ending the first loop when i reaches 0. In the second block (Reverse Number Triangle), i again steps 4, 3, 2, 1. For i = 4, j counts down 4, 3, 2, 1; for i = 3, j prints 3, 2, 1; for i = 2, j prints 2, 1; and for i = 1, j prints 1.

  • Star Pyramid i = 4..1 prints 7, 5, 3, 1 stars with 0, 1, 2, 3 leading spaces respectively.
  • Reverse Number i = 4: j runs 4 down to 1 -> "4 3 2 1 ".
  • Reverse Number i = 3..1: prints "3 2 1 ", "2 1 ", and "1 " before exiting.

3. Converting Star Pyramids to Descending Digits

ICSE papers frequently swap stars for numbers inside inverted shapes, testing whether students can adapt the inner j loop without breaking alignment. If an exam requires a left-aligned descending triangle like 5 4 3 2 1 / 5 4 3 2 / 5 4 3, keep an ascending outer loop i from 1 to n and run the inner loop j from n down to i. Conversely, if it asks for 5 4 3 2 1 / 4 3 2 1 / 3 2 1, decrement i from n down to 1 and run j from i down to 1 as shown in our second loop block. To build an inverted centered number pyramid with spaces between digits (such as 1 2 3 4 centered above 1 2 3), print one leading space per step in the s loop and print j + " " for j from 1 to i.

  • 5 4 3 2 1 / 5 4 3 2 uses i from 1 to n and j from n down to i.
  • 5 4 3 2 1 / 4 3 2 1 uses i from n down to 1 and j from i down to 1.
  • Match single-space indents with j + " " when centering spaced digits in inverted pyramids.

4. InvertedPatterns.java (BlueJ Ready)

import java.util.Scanner;

public class InvertedPatterns {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows: ");
        int n = sc.nextInt();

        System.out.println("Inverted Star Pyramid:");
        for (int i = n; i >= 1; i--) {
            for (int s = 1; s <= n - i; s++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        System.out.println("\nReverse Number Triangle:");
        for (int i = n; i >= 1; i--) {
            for (int j = i; j >= 1; j--) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
        sc.close();
    }
}

Verified Console / Dry-Run Output:

Enter number of rows: 4
Inverted Star Pyramid:
*******
 *****
  ***
   *

Reverse Number Triangle:
4 3 2 1 
3 2 1 
2 1 
1

5. Variable Description Table (VDT) for InvertedPatterns

Variable NameData TypePurpose in Program
scScannerReads the total number of rows n from the BlueJ console input
nintStores the height of both the inverted pyramid and reverse triangle
iintOuter loop counter decrementing from n down to 1 across rows
sintInner loop counter printing n - i leading spaces to center stars
jintInner loop counter printing stars or descending digits per row
🎯 1-on-1 Live Mentorship & Free Study Kit

Eliminate Loop Bugs with 4Achievers Junior

Avoid countdown-loop bugs and off-by-one errors in ICSE exams. Join 4Achievers Junior (Grades 3-12) for 1-on-1 coaching or 1:4 Micro-Pods.

💬 WhatsApp Now
🔒 Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Writing i <= 1 instead of i >= 1 in a decrementing outer loop, causing the loop body to be skipped immediately when n > 1.
  • Avoid Mistake: Using i++ instead of i-- in the outer loop, resulting in an infinite loop that hangs the BlueJ terminal.
  • Avoid Mistake: Setting the space loop condition to s <= i instead of s <= n - i, which puts maximum spaces on the widest top row and breaks the pyramid shape.
  • Avoid Mistake: Starting j at n instead of i in the reverse number triangle, which prints n numbers on every single row instead of shrinking row by row.
  • Examiner / Mentor Tip: Always verify the first and last iterations of a countdown loop manually to confirm you haven't written i <= 1 by habit.
  • Examiner / Mentor Tip: Know all three reverse-number variants (54321/4321, 54321/5432, and 12345/1234) because CISCE board papers rotate among them frequently.
  • Examiner / Mentor Tip: In a multi-pattern program, print clear header labels above each output block so the examiner can inspect both outputs immediately.

7. Interactive Concept Check Quiz

Question: For n = 5 in the Inverted Star Pyramid, how many leading spaces and stars are printed on the 3rd line?

8. Frequently Asked Questions

Why is the leading space count n - i in an inverted pyramid?
Because i starts at n on the first row, n - i equals 0, so zero leading spaces print on the widest top row. As i decreases toward 1, n - i increases by 1 each line, pushing the shrinking stars inward to stay centered.
What happens if I accidentally write i <= 1 in a countdown loop?
If you write i <= 1 when i starts at 4, the condition 4 <= 1 is immediately false, so the loop executes zero times and prints nothing. A decrementing loop counting down to 1 must always use i >= 1.
How can I print a centered inverted number pyramid instead of a left-aligned reverse number triangle?
Keep the outer loop i from n down to 1, keep the s loop from 1 to n - i, and print j + " " inside an inner loop where j runs from 1 to i (or i down to 1). Note that spacing changes to "* "-style centering when digits have spaces between them.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class