Java Number Triangle Pattern (Repeated Digits)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

Let the outer loop choose the digit; let the inner loop decide how many times to print it.

MethodIdeaBest for
Fixed nOuter i = 1..n; inner prints i for j = 1..iLabs and demos
Scanner inputSame loops; read n at runtimeInteractive practice
Spaced digitsprint(i + " ")Easier reading

Pseudocode

Pseudocode
for i from 1 to n:
    for j from 1 to i:
        print i
    new line

Cheat sheet

GoalPattern
Set sizeint n = 5;
Outer loopfor (int i = 1; i <= n; i++)
Inner loopfor (int j = 1; j <= i; j++)
Print digitSystem.out.print(i); — not j
End rowSystem.out.println();
Digits on row iExactly i copies of digit i

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach repeated digit
System.out.printlnEnds the lineAfter the inner loop finishes

Build the row with print, then break once.

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 result n = 5 · digits = 15
1
22
333
4444
55555

Worked Walkthrough — n = 3

Trace each outer value of i and how many times it is printed.

iInner runsPrinted row
11× print 11
22× print 222
33× print 3333

Key detail: the inner loop prints i, not j — that is what creates the repeat.

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();
        }
    }
}

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();
    }
}

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();
        }
    }
}

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.

Time and Space Complexity

ProgramTimeExtra space
Examples 1–3O(n²)O(1)

Total digits printed: 1 + 2 + … + n = n(n+1)/2 → still O(n²).

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.

Next: Descending Repeating Number Triangle

Continue with the descending repeat triangle in the Java number-pattern series.

Program 10 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful