A repeated number triangle prints the row digit i exactly i times — so row 1 is 1, row 2 is 22, row 3 is 333, and so on.
Remember
Rule: for i = 1 to n
print i, i times
1
22
333
4444
55555 ← n = 5
Unlike Program 5 (1, 12, 123 — print j), here you print the outer variable i on every inner iteration.
Approach
How to Solve It
Let the outer loop choose the digit; let the inner loop decide how many times to print it.
Method
Idea
Best for
Fixed n
Outer i = 1..n; inner prints i for j = 1..i
Labs and demos
Scanner input
Same loops; read n at runtime
Interactive practice
Spaced digits
print(i + " ")
Easier reading
Pseudocode
Pseudocode
for i from 1 to n:
for j from 1 to i:
print i
new line
Cheat sheet
Goal
Pattern
Set size
int n = 5;
Outer loop
for (int i = 1; i <= n; i++)
Inner loop
for (int j = 1; j <= i; j++)
Print digit
System.out.print(i); — not j
End row
System.out.println();
Digits on row i
Exactly i copies of digit i
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each repeated digit
System.out.println
Ends the line
After the inner loop finishes
Build the row with print, then break once.
Try it
Live Preview
Change n and the repeated number triangle updates instantly.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · digits = 15
1
22
333
4444
55555
Trace
Worked Walkthrough — n = 3
Trace each outer value of i and how many times it is printed.
i
Inner runs
Printed row
1
1× print 1
1
2
2× print 2
22
3
3× print 3
333
Key detail: the inner loop prints i, not j — that is what creates the repeat.
Code
Java Programs
Three complete programs: fixed n = 5, Scanner input, and spaced digits. Use View Output to reveal sample results.
Example 1 — Fixed n = 5
Hard-coded height — outer i selects the digit; inner j repeats it.
Java
public class RepeatedNumberTrianglePattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
}
}
Output
1
22
333
4444
55555
How It Works
1. Outer loop.i runs from 1 to n — the digit for that row.
2. Inner loop. Run j from 1 to i and print i each time — row 3 becomes 333.
3. Newline.println() after the inner loop starts the next row.
Example 2 — Scanner Input
Read n at runtime. Same nested-loop core.
Java
import java.util.Scanner;
public class RepeatedNumberTriangleInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int n = sc.nextInt();
if (n < 1) return;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter the number of rows: 4
1
22
333
4444
How It Works
1. Prompt and guard. Read n; exit early if it is less than 1.
2. Same loops. Only the source of n changes — print i, i times.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int n = sc.nextInt();
if (n < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — Spaced Digits
Same loops; print a space after each repeated digit for easier reading.
Java
public class RepeatedNumberTriangleSpaced {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
Output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
How It Works
1. Same shape. Outer and inner bounds match Examples 1 and 2.
2. Formatting only.i + " " separates repeats — useful when digits become two digits (n > 9).
3. Trailing space. Each row may end with a space; that is normal for this simple style.
Edge Cases & Pitfalls
Check these before calling the solution done.
print(j)
Wrong pattern
Printing j instead of i gives 1, 12, 123 — that is Program 5.
println inside
Vertical digits
Calling println inside the inner loop prints one digit per line. Keep it after the inner loop.
Outer reverse
Descending repeat
Counting i from n down to 1 yields Program 10 (5, 44, 333…).
n = 1
Single digit
Output is just 1 on one line.
n ≤ 0
Empty output
The outer loop never runs. Guard interactive input with n >= 1.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Examples 1–3
O(n²)
O(1)
Total digits printed: 1 + 2 + … + n = n(n+1)/2 → still O(n²).
Remember
Key Takeaways
Rule: outer i = 1..n; print i exactly i times.
print(i): not j — that single choice makes the repeats.
Vs Program 10: flip the outer loop for the descending repeat triangle.
Complexity:O(n²) for n rows.
One line: on row i, print the digit i exactly i times.
Frequently Asked Questions
The outer loop sets i = 4. The inner loop runs j from 1 to 4 and each iteration prints i, so digit 4 appears four times.
The outer loop runs i from 1 to n. For each i, the inner loop runs j from 1 to i and prints i, then println ends the row.
Reverse the outer loop: for (int i = n; i >= 1; i--) and keep the inner loop j = 1..i with print(i). That yields 5, 44, 333, 2222, 11111 — see Program 10.
Program 5 prints ascending digits 1, 12, 123 (print j). Program 9 repeats the row digit: 1, 22, 333 (print i).
Yes. Use System.out.print(i + " ") inside the inner loop — see Example 3.
O(n²) for n rows. Total printed digits are 1+2+…+n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
Outer loop sets row digit i; inner loop prints i exactly i times — 1, 22, 333, … O(n²) for n rows.