A Palindromic Number Pyramid (1 / 121 / 12321) places three sequential inner loops inside an outer loop i from 1 to n. First, loop s prints n - i leading spaces to center the row. Second, ascending loop j prints digits from 1 up to i. Third, descending loop k prints digits from i - 1 down to 1, completing the mirror image without duplicating peak digit i.
- 1. Decomposing Each Row into Spaces, Ascent, and Descent
- 2. Four-Row Dry Run Showing Peak Non-Duplication
- 3. Single-Digit vs Multi-Digit Alignment Constraints
- 4. PalindromicPyramid.java (BlueJ Ready)
- 5. Variable Description Table (VDT) for PalindromicPyramid
- 6. Common Mistakes & Top Scorer Tips
- 7. Interactive Concept Check Quiz
- 8. Frequently Asked Questions
1. Decomposing Each Row into Spaces, Ascent, and Descent
A palindromic string reads identically forward and backward around a central axis. To render a palindromic number pyramid on screen, avoid trying to calculate digits with a single mathematical formula; instead, split every row i into three consecutive horizontal segments inside one outer loop. First, the margin loop s prints n - i spaces so row i centers over the base. Second, the ascending loop j runs from 1 up to i, printing 1, 2, ..., i and placing the peak value i right in the middle. Third, the descending loop k begins at i - 1 and steps down to 1, printing i - 1, ..., 2, 1 on the right flank. Because all three loops sit sequentially inside the outer loop i rather than inside one another, Java draws left padding, left slope, and right slope cleanly on the same line.
- Loop s (1 to n - i): shifts the starting column inward for pyramid alignment.
- Loop j (1 to i): prints the ascending left half including the central peak i.
- Loop k (i - 1 down to 1): prints the descending right half without repeating i.
2. Four-Row Dry Run Showing Peak Non-Duplication
Trace every step for n = 4 to confirm peak non-duplication. On row i = 1, s prints 3 spaces, j prints "1", and k starts at 1 - 1 = 0 (0 >= 1 is false, so k is skipped), producing " 1". On row i = 2, s prints 2 spaces, j loops 1 to 2 printing "12", and k starts at 1 down to 1 printing "1", giving " 121". On row i = 3, s prints 1 space, j prints "123", and k counts 2 then 1 ("21"), resulting in " 12321". On the final row i = 4, s prints 0 spaces (4 - 4 = 0), j prints "1234", and k iterates 3, 2, 1 to append "321", completing "1234321". Every single line contains 2 * i - 1 digits and exhibits exact left-right reflection around digit i.
- Row 1 (i = 1): 3 spaces + "1" + "" (k skipped) = " 1"
- Row 2 (i = 2): 2 spaces + "12" + "1" = " 121"
- Row 3 (i = 3): 1 space + "123" + "21" = " 12321"
- Row 4 (i = 4): 0 spaces + "1234" + "321" = "1234321"
3. Single-Digit vs Multi-Digit Alignment Constraints
Because single-digit characters '1' through '9' each occupy exactly one column width, tight unspaced output ("12321") aligns cleanly with single-space indents (" ") up to n = 9. Once n reaches 10 or more, two-digit integers like 10 occupy two character widths and push subsequent rows out of alignment unless fixed-width formatting is applied. If an ICSE exam requests spaced digits ("1 2 3 2 1"), remember the golden width rule: doubling the character width of each printed digit from 1 char (j) to 2 chars (j + " ") requires doubling the leading indent width from " " to " " in the s loop. Another common ICSE twist reverses the pyramid's values so row i counts down from i to 1 and back up to i (1 / 212 / 32123), achieved by running j from i down to 1 and k from 2 up to i.
- Unspaced digits align symmetrically up to n = 9 using single-space indents.
- For spaced digits (j + " "), change the space loop print to " " (two spaces).
- Swap loop directions (j: i down to 1, k: 2 up to i) to produce 1 / 212 / 32123.
4. PalindromicPyramid.java (BlueJ Ready)
import java.util.Scanner;
public class PalindromicPyramid {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows (1-9): ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
for (int k = i - 1; k >= 1; k--) {
System.out.print(k);
}
System.out.println();
}
sc.close();
}
}
Verified Console / Dry-Run Output:
Enter number of rows (1-9): 5
1
121
12321
1234321
123454321
5. Variable Description Table (VDT) for PalindromicPyramid
| Variable Name | Data Type | Purpose in Program |
|---|---|---|
| sc | Scanner | Reads the pyramid height n entered by the user via keyboard input |
| n | int | Stores the total number of rows in the palindromic pyramid |
| i | int | Outer loop counter controlling the current row and peak digit from 1 to n |
| s | int | First inner loop counter printing n - i leading spaces per row |
| j | int | Second inner loop counter printing ascending digits from 1 to i |
| k | int | Third inner loop counter printing descending mirror digits from i - 1 to 1 |
Conquer Multi-Loop ICSE Java Programs
Master multi-loop decomposition and ICSE Class 10 pattern logic in 1-on-1 sessions or 1:4 Micro-Pods with 4Achievers Junior (Grades 3-12).
6. Common Mistakes & Top Scorer Tips
- Avoid Mistake: Starting the descending loop at k = i instead of k = i - 1, which duplicates the center peak digit (printing 1221 and 123321 instead of 121 and 12321).
- Avoid Mistake: Placing the descending k loop inside the ascending j loop rather than sequentially after it, creating deeply nested garbage output.
- Avoid Mistake: Printing spaces inside the j and k loops while printing only single spaces in the s loop, breaking pyramid symmetry.
- Avoid Mistake: Using k++ instead of k-- in the third inner loop, causing an infinite loop once i >= 2.
- Examiner / Mentor Tip: Write all three inner loops (s, j, k) clearly indented at the same level inside the outer i loop so examiners immediately see sequential decomposition.
- Examiner / Mentor Tip: Verify row 1 (i = 1) in your dry run to demonstrate that the k loop naturally skips when i - 1 == 0 without needing an extra if statement.
- Examiner / Mentor Tip: Note the total digit count per row (2 * i - 1) when explaining peak non-duplication during viva questioning.
7. Interactive Concept Check Quiz
Question: If a student mistakenly writes for (int k = i; k >= 1; k--) instead of k = i - 1, what will row 4 output?
8. Frequently Asked Questions
What does the third loop (k = i - 1) do on row 1 when i = 1?
Should the descending k loop be placed inside the j loop?
How do I format the pyramid with spaces between each digit?
Explore Related Lessons & Free Interactive Tools
- All 110 Guides in ICSE Java BlueJ Library
- Butterfly Star Pattern in Java Using Nested Loops
- Fibonacci Series Program in Java (Iterative Terms & Sum)
- Tribonacci & Pell Series Program in Java (Recurrence & Trace)
- Interactive Tool: ICSE Class 10 Java BlueJ Output Quiz
- Interactive Tool: ICSE Variable Description Table (VDT) Auto-Formatter
