Java Repeating Number Pattern (Shrinking)

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

What Is This Pattern?

A shrinking repeating number pattern starts with many copies of 1, then raises the digit while the row gets shorter — so you see 11111, 2222, 333, and so on.

Remember
Rule: outer i = 1..rows; inner j = i..rows; print i

11111
2222
333
44
5          ← rows = 5

Unlike Program 10 (digit falls while length grows) or Program 11 (digit and count both equal i counting down), this pattern counts the digit up while the row shrinks.

How to Solve It

Outer loop from 1 to rows. Inner loop from current i to rows, printing i each time. End the line after the inner loop.

MethodIdeaBest for
Nested loopsOuter up, inner i..rows, print iLearning, interviews, exams
String.repeatOne println per rowWhen the bounds already make sense

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i to rows:
        print i
    print newline

Cheat sheet

GoalPattern
Outer (digit rises)for (int i = 1; i <= rows; i++)
Inner (repeat count)for (int j = i; j <= rows; j++) System.out.print(i);
End the rowSystem.out.println();
Repeats on row irows - i + 1
ShortcutString.valueOf(i).repeat(rows - i + 1)
Vs Program 10Same length rule; digit counts up, not down

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit i
System.out.printlnEnds the current lineAfter the inner loop

Print digits without a newline, then end the row once.

Live Preview

Change the row count and the shrinking repeating pattern updates instantly.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 15 digits
11111
2222
333
44
5

Worked Walkthrough

Trace three outer values when rows = 5 — watch the digit rise while the repeat count falls.

Outer iInner jRepeatsPrints
11..5511111
33..53333
55..515

The printed value is always the outer i; the inner loop only controls how many times it appears.

Java Programs

Three complete programs: fixed rows = 5, Scanner input, and a String.repeat shortcut. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — outer up from 1, inner from i to 5, print i.

Java
public class ShrinkingRepeatingPattern {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = i; j <= rows; j++) {
                System.out.print(i);
            }
            System.out.println();
        }
    }
}

How It Works

1. Outer sets the digit. i runs from 1 to rows — that value is what you print.

2. Inner sets the count. j runs from i to rows, so the digit appears rows - i + 1 times.

3. End the row. Call System.out.println() only after the inner loop finishes.

Example 2 — User Input Rows

Read the row count at runtime. Prefer hasNextInt() before nextInt() in real apps.

Java
import java.util.Scanner;

public class ShrinkingRepeatingPatternInput {
    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 = i; j <= rows; j++) {
                System.out.print(i);
            }
            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Same loop core. Only the source of rows changes from a literal to Scanner.

2. Entering 4 stops early. You get four rows ending at a single 4.

3. Validate in real apps. Prefer checking hasNextInt() and requiring a positive height (tip below).

Safer input tip
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive whole number.");
    return;
}
int rows = sc.nextInt();
if (rows < 1) {
    System.out.println("Enter a positive whole number.");
    return;
}

Example 3 — String.repeat Shortcut

Build each row in one call once the rows - i + 1 count is clear.

Java
public class ShrinkingRepeatingString {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            System.out.println(String.valueOf(i).repeat(rows - i + 1));
        }
    }
}

How It Works

1. Same outer loop. i still counts up from 1 to rows.

2. Count formula. rows - i + 1 is exactly how many times the nested inner loop would run.

3. Learn loops first. Prefer Examples 1–2 in class; use repeat as a later shortcut (Java 11+).

Edge Cases & Pitfalls

Check these before calling the solution done.

Print value

Print i, not j

Printing j reprints a left-shifted ascending run, not a repeated-digit pattern.

Inner bound

Inner must run i..rows

Using 1..i instead reprints Program 9 (1, 22, 333), not this shape.

println inside

Do not put println inside the inner loop

That prints one digit per line and destroys the pattern.

Bad input

Validate with hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require rows >= 1.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(1)
String.repeat (Example 3)O(n²)O(n) per row string

Digits printed are n + (n − 1) + … + 1 = n(n + 1) / 2 — quadratic in n. Direct prints need only the loop variables; repeat builds a short-lived row string.

Key Takeaways

  • Rule: outer i = 1..rows, inner j = i..rows, print i.
  • Shrink: each row drops one copy while the digit increases.
  • Break the row: call println only after the inner loop finishes.
  • Complexity: O(n²) time; O(1) extra space with direct prints.

One line: print digit i exactly rows - i + 1 times while i counts up.

Frequently Asked Questions

A shrinking repeating number pattern: for rows=5 you get 11111, 2222, 333, 44, 5.
The outer loop increases i from 1 to rows. The inner loop runs from i to rows, so it executes (rows - i + 1) times — fewer repeats as i grows.
System.out.print(i) stays on the same line. System.out.println() ends the current line. Digits use print; the row break uses println after the inner loop.
Program 10 counts the digit down (5, 44, 333). Program 12 counts the digit up while the row still shrinks (11111, 2222, 333).
Program 11 repeats i exactly i times counting down (55555, 4444). Program 12 uses inner i..rows so digit rises while length falls.
Yes. System.out.println(String.valueOf(i).repeat(rows - i + 1)) prints a full row in one call. Nested loops are better for learning; repeat is a handy shortcut later.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
One row prints a single digit 1.

Did you know?

Row digit i repeats rows − i + 1 times. The outer loop counts up from 1, while the inner loop runs from i to rows, so each row shrinks — still O(n²) total prints.

Next: Alternating Zigzag Triangle

Continue with the next pattern in the Java number-pattern series.

Program 13 tutorial →

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