The classic right-angled number triangle (1 / 1 2 / 1 2 3) uses two nested for loops where the outer loop i iterates from 1 to rows and the inner loop j resets to 1 on every row and runs while j <= i. Printing j displays ascending column values starting from 1 on every line, whereas printing i repeats the current row number i times across that line.
- 1. Resetting the Inner Counter on Every New Row
- 2. Tracing Loop Bounds for n = 5 Rows
- 3. Printing j vs Printing i: Two ICSE Variants
- 4. NumberTriangle.java (BlueJ Ready)
- 5. Variable Description Table (VDT) for NumberTriangle
- 6. Common Mistakes & Top Scorer Tips
- 7. Interactive Concept Check Quiz
- 8. Frequently Asked Questions
1. Resetting the Inner Counter on Every New Row
Every right-angled number triangle begins with one core idea: the inner loop's upper limit depends directly on the outer loop's current value. When the outer loop assigns i = 1, the inner loop runs from j = 1 up to 1; when i advances to 2, j resets to 1 and runs up to 2; and so on until i reaches rows. Because the initialization expression int j = 1 sits inside the inner for header, Java re-creates and resets j to 1 on every single pass of the outer loop. This automatic reset guarantees that every row starts fresh at 1 rather than carrying forward past values like Floyd's Triangle does. Meanwhile, the System.out.println() placed immediately after the inner loop closes forces each numbered sequence onto its own line in the BlueJ terminal.
- Outer loop i tracks which row is currently under construction (1 through rows).
- Inner loop j re-initializes to 1 on every outer iteration because its header executes anew each row.
- Loop termination condition j <= i ensures row i prints exactly i numbers.
2. Tracing Loop Bounds for n = 5 Rows
Walk through a complete dry run with rows = 5. At i = 1, j begins at 1, checks 1 <= 1 (true), prints "1 ", increments to 2, fails 2 <= 1, and triggers println(). At i = 2, j resets to 1, runs for j = 1 and j = 2 to print "1 2 ", then breaks at j = 3. At i = 3, j prints "1 2 3 " before exiting at j = 4. At i = 4, j iterates four times printing "1 2 3 4 ". On the final outer iteration i = 5, j covers 1, 2, 3, 4, and 5 to output "1 2 3 4 5 ", exits at j = 6, prints a newline, and then i increments to 6 which terminates the outer loop. Counting every print call confirms 1 + 2 + 3 + 4 + 5 = 15 numbers printed across 5 lines.
- Row 1 (i = 1): j takes value 1 -> prints 1
- Row 2 (i = 2): j takes values 1, 2 -> prints 1 2
- Row 3 (i = 3): j takes values 1, 2, 3 -> prints 1 2 3
- Rows 4 & 5 follow identically up to 1 2 3 4 5 before i = 6 ends the program.
3. Printing j vs Printing i: Two ICSE Variants
ICSE Computer Applications papers frequently test whether students understand the difference between the two loop indices inside a nested block. Printing j displays the changing column counter (1 / 1 2 / 1 2 3), whereas changing just one character to print i displays the constant row counter (1 / 2 2 / 3 3 3). A third popular board variation prints rows in descending digits per line (1 / 2 1 / 3 2 1) by keeping the outer loop i from 1 to rows and reversing the inner header to for (int j = i; j >= 1; j--). Before submitting any BlueJ solution, check whether the sample output leaves spaces between digits ("1 2 3") or prints them flush ("123"), and adjust j + " " versus j accordingly.
- Print j for column-incrementing rows (1 / 1 2 / 1 2 3).
- Print i for row-repeating patterns (1 / 2 2 / 3 3 3).
- Reverse the inner loop (j = i; j >= 1; j--) to output 1 / 2 1 / 3 2 1.
4. NumberTriangle.java (BlueJ Ready)
import java.util.Scanner;
public class NumberTriangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
sc.close();
}
}
Verified Console / Dry-Run Output:
Enter number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
5. Variable Description Table (VDT) for NumberTriangle
| Variable Name | Data Type | Purpose in Program |
|---|---|---|
| sc | Scanner | Reads the number of rows entered by the user via standard input |
| rows | int | Stores the total height of the number triangle |
| i | int | Outer loop counter controlling the current row from 1 to rows |
| j | int | Inner loop counter resetting to 1 per row and printing digits up to i |
Learn Java Nested Loops Step-by-Step
Build rock-solid Java fundamentals with 4Achievers Junior (Grades 3-12). Schedule a free 1:1 trial or join our small 1:4 Micro-Pods for ICSE exam mastery.
6. Common Mistakes & Top Scorer Tips
- Avoid Mistake: Declaring int j = 1 outside the inner loop and omitting re-initialization, which prints only row 1 properly and breaks subsequent rows.
- Avoid Mistake: Printing i instead of j when the exam question specifies 1 / 1 2 / 1 2 3, producing 1 / 2 2 / 3 3 3 instead.
- Avoid Mistake: Using j < i instead of j <= i, which leaves the first row blank and prints one fewer number on every row.
- Avoid Mistake: Omitting System.out.println() after the inner loop, merging the entire triangle onto a single horizontal line.
- Examiner / Mentor Tip: Expect follow-up viva questions asking you to switch 1 / 1 2 / 1 2 3 into 1 / 2 2 / 3 3 3 on the spot—only the variable inside System.out.print() changes.
- Examiner / Mentor Tip: If the question asks for a menu-driven program with two patterns, wrap each pattern inside a switch(choice) case cleanly.
- Examiner / Mentor Tip: Keep variable names short and meaningful (rows, i, j) so your VDT table is quick and unambiguous to write under exam time limits.
7. Interactive Concept Check Quiz
Question: If you modify NumberTriangle so the inner loop prints i instead of j, what will the 4th row display?
8. Frequently Asked Questions
What changes if I replace System.out.print(j + " ") with System.out.print(i + " ")?
How many times does the inner loop body execute when rows = 5?
How do I print each row in reverse order such as 1 / 2 1 / 3 2 1?
Explore Related Lessons & Free Interactive Tools
- All 110 Guides in ICSE Java BlueJ Library
- Inverted Pyramid & Reverse Number Patterns in Java
- Alphabet Triangle Patterns (A / AB / ABC) in Java
- Binary 1-0-1 Triangle Pattern Program in Java
- Interactive Tool: ICSE Class 10 Java BlueJ Output Quiz
- Interactive Tool: ICSE Variable Description Table (VDT) Auto-Formatter
