C# Repeating Number Pattern (Shrinking)

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

What Is This Pattern?

A shrinking repeating number pattern prints digit i repeated rows - i + 1 times, counting i up from 1 so each row gets shorter.

Remember
Rule: for i = 1..n, print i exactly (n - i + 1) times

11111
2222
333
44
5       ← 5 rows

In C# you solve it with two nested for loops: the outer loop picks the digit, the inner loop runs from i to rows with Console.Write, then WriteLine() ends the row. Program 11 is the inverted twin (55555, 4444, 333…).

How to Solve It

Two ways to emit the same shape — start with nested loops, then optionally shorten with new string.

MethodIdeaBest for
Nested loopsOuter picks digit 1..n; inner runs i..n so width shrinksLearning, interviews, exams
new stringBuild a whole repeated-digit row in one callShorter demos once loops click

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from i to rows:
        print i (no newline)
    print newline

Cheat sheet

GoalPattern
Pick digit (count up)for (i = 1; i <= rows; i++)
Shrink repeatsfor (j = i; j <= rows; j++) Console.Write(i);
End the rowConsole.WriteLine();
One-line row shortcutConsole.WriteLine(new string(i.ToString()[0], rows - i + 1));
Inverted twin (Program 11)for (i = rows; i >= 1; i--) with Write(i) × i

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach repeat of digit i
Console.WriteLineEnds the current lineAfter the inner loop

Live Preview

Change the row count and the shrinking pattern updates instantly — including the total digit count.

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

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

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, how many times the inner loop runs, and the printed row.

iInner jRepeatsPrinted row
11..441111
22..43222
33..4233
44..414

Total digits are 4+3+2+1 = 10 — the triangular number n(n+1)/2.

C# Programs

Three complete programs: fixed height, row-count input, and a new string shortcut. Use View Output for sample results.

Example 1 — Fixed rows = 5

Outer loop counts up; inner loop runs from i to rows so each row shrinks.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 5;
            int i, j;

            for (i = 1; i <= rows; i++)
            {
                for (j = i; j <= rows; j++)
                {
                    Console.Write(i);
                }
                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Count up. for (i = 1; i <= rows; i++) yields 1, 2, 3, 4, 5 — the digit for each row.

2. Shrink width. When i = 1, the inner loop runs five times (11111); when i = 5, just once (5).

3. Newline. WriteLine() after the inner loop starts the next shorter row.

Example 2 — User Input

Read the row count at runtime. Prefer int.TryParse in real apps so bad input does not throw.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows;
            int i, j;

            Console.Write("Enter the number of rows: ");
            if (!int.TryParse(Console.ReadLine(), out rows) || rows < 1)
            {
                Console.WriteLine("Please enter a positive whole number.");
                return;
            }

            for (i = 1; i <= rows; i++)
            {
                for (j = i; j <= rows; j++)
                {
                    Console.Write(i);
                }
                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Validate rows. TryParse rejects non-numeric input; require rows >= 1.

2. Same nested loops. Four rows start at 1111 and end at 4.

Example 3 — new string Shortcut

Build each repeated-digit row in one call. Best for single-digit rows (1–9).

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows = 5;

            for (int i = 1; i <= rows; i++)
            {
                Console.WriteLine(new string(i.ToString()[0], rows - i + 1));
            }
        }
    }
}

How It Works

1. Repeat a char. new string(i.ToString()[0], rows - i + 1) builds a string of the digit character repeated the right count.

2. Same shape. Identical output to Example 1 — keep the two-loop version for exams that ask for both bounds.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong twin

for (j = 1; j <= i; j++)

That grows repeats (1, 22, 333 — Program 9). Keep j from i to rows so width shrinks.

WriteLine early

WriteLine(i) inside the inner loop

That puts each digit on its own line. Use Write(i); call WriteLine() only after the row finishes.

wrong start

j = 1 instead of j = i

Starting at 1 every time makes every row length rows — you get a rectangle, not a shrinking triangle.

rows = 1

Single digit

Output is just 1 — a good sanity check.

rows ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

multi-digit

rows > 9

Write(10) prints two characters. Labs usually stick to 1–9; the new string shortcut also assumes a single digit char.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(n²)O(1)
new string shortcut (Example 3)O(n²)O(n) per row for the built string

There are n rows printing n+(n-1)+…+1 digits, so total work is n(n+1)/2.

Key Takeaways

  • Rule: for i = 1..n, print digit i exactly n - i + 1 times.
  • Inner bound: j from i to rows shrinks the width as the digit grows.
  • Write vs WriteLine: repeats stay on the line; WriteLine advances after each row.
  • Next step: Program 13 introduces an alternating zigzag number triangle.

One line: for each digit from 1 to n, print it n-i+1 times with Write, then WriteLine.

Frequently Asked Questions

The inner loop runs from j = i to rows. As i increases (1, 2, 3, …), the inner loop runs fewer times, so each row prints fewer copies of the row digit.
On the first row, i = 1 and the inner loop runs from 1 to rows, so it prints 1 exactly rows times — five 1s when rows = 5.
Console.Write stays on the same line. Console.WriteLine ends the current line. Repeated digits use Write(i); the row break uses WriteLine after the inner loop.
Use the inner loop as 1..i instead of i..rows so the repeat count grows (see Program 9).
Program 11 counts the outer loop down and repeats digit i exactly i times (55555, 4444, 333, 22, 1). Program 12 counts up and uses for (j = i; j <= rows; j++) so digit i repeats rows - i + 1 times (11111, 2222, 333, 44, 5).
O(n²) where n is the number of rows. Total Console.Write calls equal n+(n-1)+…+1 = n(n+1)/2.
Yes. Console.WriteLine(new string(i.ToString()[0], rows - i + 1)) prints a full single-digit row in one call. Nested loops are better for learning; the string constructor is a handy shortcut later.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

Did you know?

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

Next: Alternating Zigzag Triangle

Mix directions on each row for a zigzag number triangle.

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