Repeat Digit
print(i)
Row i prints the digit i exactly i times — e.g. row 3 → 333.

The repeated number triangle prints 1, 22, 333, 4444, 55555 — row i repeats digit i exactly i times. This tutorial covers nested-loop logic, live preview, worked Java examples, edge cases, and O(n²) complexity.
print(i)
Row i prints the digit i exactly i times — e.g. row 3 → 333.
i = 1..rows
Outer loop picks the digit for each row — grows from 1 to rows.
j = 1..i
Inner loop controls repeat count — row length equals outer index i.
print j vs i
Program 5 prints ascending digits; this repeats the row digit instead.
1–20 rows
Pick a row count and draw the repeated triangle instantly in the browser.
Complexity
Total digits ≈ n(n+1)/2 — quadratic time; extra memory stays O(1).
A repeated number triangle grows each row by one more copy of the same digit: 1, 22, 333, up to 55555 for five rows.
In Java: outer loop for (i = 1; i <= rows; i++), inner loop for (j = 1; j <= i; j++) System.out.print(i), then println().
It teaches the difference between printing loop index j (Program 5) and repeating row digit i — then continue to Program 10 for the descending repeat variant.
Inner loop prints the row digit, not j.
Inner loop runs i times on row i.
Program 5: 1, 12, 123. Program 9: 1, 22, 333.
Total prints grow as n(n+1)/2.
In short: for each i from 1 to rows, print i exactly i times, then System.out.println().
Given a positive integer rows (e.g. 5), print a repeated number triangle: row i repeats digit i exactly i times (e.g. row 3 → 333).
// rows = 5 (conceptual shape)
// 1
// 22
// 333
// 4444
// 55555 | Item | Type | Description |
|---|---|---|
rows | int | Number of triangle lines — outer loop runs from 1 up to rows. |
i | int | Outer loop — current row digit; grows from 1 to rows. |
j | int | Inner loop — runs j from 1 to i; prints i each time. |
for i from 1 to rows:
for j from 1 up to i:
print i
print newline | Approach | Idea | Best for |
|---|---|---|
| Nested loops | 1, 22, 333, … | Learning and interviews |
| User-input rows | sc.nextInt(); | Flexible console programs |
| Spaced output | System.out.print(i + " ") | Easier reading per row |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = 1; i <= rows; i++) |
| Repeat digit i | for (j = 1; j <= i; j++) System.out.print(i); |
| End the row | System.out.println(); |
| Spaced digits | System.out.print(i + " "); |
| User input | sc.nextInt(); |
| Program 5 contrast | Program 5 prints ascending digits (print j); Program 9 repeats row digit (print i) |
How outer i and inner repeat count j = 1..i with print(i) work together.
for (i = 1; i <= rows; i++)Selects row digit — grows from 1 to rows.
for (j = 1; j <= i; j++)Repeats digit i exactly i times.
print(i)Print row digit i each iteration — 333 not 123.
trace i=3Dry-run when i=3: inner loop runs 3 times → prints 333.
Reach for this pattern when teaching nested loops, growing inner bounds, and concatenated digit output.
Natural follow-up in the repeat-digit series — compare growing reverse (Program 7/8) with same-digit rows.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible row count.
Compare with Program 5 (ascending digits), then continue to Program 10 (descending repeat triangle).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Enter a row count and draw the repeated number triangle in the browser.
Three complete Java programs — fixed rows = 5, Scanner input, and a spaced-output variant. Click View Output to reveal sample console results.
Print five rows — inner loop repeats digit i exactly i times.
rows = 5Hard-coded row count — outer i selects the digit; inner j controls how many times to print it.
public class RepeatedNumberTrianglePattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
}
} When i = 3, the inner loop runs three times and prints i each time — output 333. When i = 5, output is 55555.
Read the row count with Scanner instead of hard-coding 5.
Read rows with Scanner.nextInt(); same nested loops as Example 1.
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 rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
sc.close();
}
} Same nested-loop core as Example 1; only the source of rows changes.
Add spaces between repeated digits for easier reading.
Print a space after each repeated digit with print(i + " ").
public class RepeatedNumberTriangleSpaced {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}
} Same j = 1..i logic with print(i); only the output format adds spaces between digits.
System.out is built in; use Scanner when reading input. Set loop variables i, j with rows = 5.
for (i = 1; i <= rows; i++) — each row repeats digit i one more time.
for (j = 1; j <= i; j++) — repeats digit i exactly i times.
System.out.println() ends the row after the inner loop finishes.
Each row adds one more copy of the row digit — O(n²) time, O(1) extra memory.
i = 3 (rows = 5)Trace how increasing i adds one more copy of the row digit.
Row i | j range | Output |
|---|---|---|
| 1 | 1..1 | 1 |
| 2 | 1..2 | 22 |
| 3 | 1..3 | 333 |
| 4 | 1..4 | 4444 |
| 5 | 1..5 | 55555 |
After the inner loop, println() moves to the next row.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: compare inner bounds with Program 5 and Program 8 and watch digit order change.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 10 for the descending repeat triangle.
Practice System.out.print vs System.out.println() without complex math.
Example: put System.out.println() inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use System.out.print(i + " ") between digits on each row.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for rows = 5 — total is still 15 (1+2+3+4+5)).
Pair the pattern with Scanner return checks and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner Java courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace i and j on paper for rows = 3 before coding — watch how each row grows by one digit.
Small habits that keep number-pattern code clean.
Outer loop counts down; inner loop must run j = 1..i so each row grows on the right.
ScannerCall sc.hasNextInt() so bad input does not leave rows uninitialized.
Only call System.out.println() after the inner loop finishes the row.
for (j = 1; j <= i; j++) System.out.print(i) concatenates digits on one line.
Trace i = 1, 2, 3 on paper before coding the full rows = 5 demo.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put System.out.println() inside the inner loop.
Mistakes that commonly break Repeated Number Triangle Pattern patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use System.out.print(i) for digits; System.out.println() only after the inner loop.
for (int i = 1; i <= rows; i++) with j = i..1 prints 1, 21, 321 — not 5, 54, 543.
→ Use for (int i = rows; i >= 1; i--) and for (j = 1; j <= i; j++).
for (j = i; j >= 1; j--) builds 1, 21, 321 — not this reverse-growing pattern.
→ Use for (j = 1; j <= i; j++) so each row starts at rows.
Program 4 prints the longest row first (54321). Program 8 prints the shortest row first (5) with the same j = 1..i range.
→ Check output order: growing rows need i counting down from rows.
Letters or empty input leave rows uninitialized.
→ Call sc.hasNextInt() and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 on one line.
Outer loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: 1 and 21.
Unchecked Scanner input leaves rows unset — call hasNextInt() first.
Each row prints i digits — total work grows as n(n+1)/2.
Try these variations to lock in the pattern.
1, 12, 123 (print j)for (i = rows; i >= 1; i--)5, 44, 333, 2222, 111115, 44, 333, 2222, 11111System.out.print(i + " ") between digitsi = 1..rows. Inner loop: j = 1..i with print(i).System.out.print stays on the line; System.out.println() advances — mix them carefully.rows > 0 for interactive programs; rows = 1 should print a single 1.i prints exactly i copies of digit i — compare with Program 5 where inner loop prints j.Quick Takeaway: outer loop i = 1..rows, inner loop j = 1..i with System.out.print(i), then System.out.println().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Spaced output (Example 3) | O(n²) | O(1) |
The Repeated Number Triangle Pattern is a compact nested-loop lesson: outer loop lowers i while the inner loop prints digits rows down to i. Master the fixed-rows version, then try user input and spaced output.
Practice the three examples above, then continue to Program 10 for the descending repeating triangle (5, 44, 333, 2222, 11111).
Each row prints rows..i — keep System.out.print(i) for digits and System.out.println() for the row break.
for (i = 1; i <= rows; i++) in the outer loopfor (j = 1; j <= i; j++) prints digits i..1System.out.print(i) for digits and System.out.println() after each rowrows ≥ 1 for interactive programssc.hasNextInt() before using rowsSystem.out.println() inside the inner digit loopj-- from iprintln() inside the inner loop — digits must stay on one rowrows = 1 edge casePrint the pattern the beginner-friendly way.
Row i prints i repeated i times
DefinitionRepeats digit i
Codej = 1..i, print i
CodeEnds each row
ShapeO(n²) time
AnalysisOuter loop sets row digit i; inner loop prints i exactly i times — 1, 22, 333, … O(n²) for n rows.
Move on to the descending repeating number triangle in the Java number-pattern series.
12 people found this page helpful