C++ Number Triangle Pattern (Left-Shifted)

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

What Is This Pattern?

A left-shifted descending number triangle starts each row at i and prints through rows, so the leftmost digit drops off one by one.

Remember
Rule: for i from 1 to rows
        print j from i to rows

12345
2345
345
45
5     ← rows = 5

Follows the descending 1..i triangle in Program 1; next is the reverse descending triangle in Program 3.

How to Solve It

Outer loop grows i from 1 to rows. Inner loop starts at i (not 1) and prints through rows.

MethodIdeaBest for
Start at ifor (j = i; j <= rows; j++) cout << j;Learning, interviews, exams
String rowAppend digits to a string, then one cout per rowSame shape, one print per line

Pseudocode

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

Cheat sheet

GoalPattern
Grow start digitfor (i = 1; i <= rows; i++)
Print i..rowsfor (j = i; j <= rows; j++) cout << j;
End of rowcout << "\n";
Add spacescout << j << " ";
Program 1 shapefor (i = rows; i >= 1; i--) with j = 1..i

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << jStays on the same lineEach digit on the row
cout << "\n"Ends the current lineAfter the inner loop finishes

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

Live Preview

Change the row count and the left-shifted triangle updates instantly — capped at 9 for readable demos.

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
12345
2345
345
45
5

Worked Walkthrough — rows = 5

Trace how raising the inner start i drops the leftmost digit each row.

iInner rangePrints
1j = 1..512345
2j = 2..52345
3j = 3..5345
4j = 4..545
5j = 5..55

Total digits = n + (n-1) + … + 1 = n(n+1)/2 → O(n²).

C++ Programs

Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — inner loop starts at i and prints through rows.

C++
#include <iostream>
using namespace std;

int main()
{
    int rows = 5;
    int i, j;

    for (i = 1; i <= rows; i++)
    {
        for (j = i; j <= rows; j++)
            cout << j;
        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer picks the start. i is both the row index and the first digit of that row.

2. Inner prints i..rows. for (j = i; j <= rows; j++) cout << j; — not j = 1.

3. Newline once. Call cout << "\n" only after the inner loop finishes.

Example 2 — User Input Rows

Read rows with cin, validate, then use the same i..rows core.

C++
#include <iostream>
using namespace std;

int main()
{
    int rows;
    int i, j;

    cout << "Enter the number of rows: ";
    if (!(cin >> rows) || rows <= 0)
    {
        cout << "Please enter a positive integer.\n";
        return 1;
    }

    for (i = 1; i <= rows; i++)
    {
        for (j = i; j <= rows; j++)
            cout << j;
        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and non-positive values before printing.

2. Same core. Start-at-i matches Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable output:

Safer input
if (!(cin >> rows) || rows < 1 || rows > 9)
{
    cout << "Enter a whole number from 1 to 9.\n";
    return 1;
}

Example 3 — Compact rows = 3

Same structure with only three rows — easy to confirm the shifting start.

C++
#include <iostream>
using namespace std;

int main()
{
    int rows = 3;
    int i, j;

    for (i = 1; i <= rows; i++)
    {
        for (j = i; j <= rows; j++)
            cout << j;
        cout << "\n";
    }

    return 0;
}

How It Works

1. Three rows. i = 1 → 123; i = 2 → 23; i = 3 → 3.

2. Trace on paper. If you start j at 1 instead of i, you get Program 1’s ascending triangle — not this shift.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

j = 1

Wrong start

Starting at j = 1 prints 1..rows every row. For this shape keep j = i.

\n inside

Broken rows

If cout << "\n" sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.

rows = 1

Single digit

Output is just 1 — a good sanity check for input validation.

rows > 9

Multi-digit glue

Digits run together (…91011…). Cap demos or add spaces with cout << j << " ".

cout << i

Repeating rows

Printing i instead of j fills each row with the same digit. Print the loop variable j.

cin

Check the stream

Validate cin >> rows before looping — a failed read leaves rows unset or stale.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Total digits printed = n + (n-1) + … + 1 = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Start at i: the inner loop begins at j = i, not j = 1.
  • Print through rows: each row is digits i..rows, so the left edge shifts.
  • Break the row: cout << j in the inner loop; cout << "\n" after it.
  • Complexity: O(n²) time from n(n+1)/2 digits; O(1) extra space.

One line: for each i from 1 to rows, print j from i to rows, then newline.

Frequently Asked Questions

A left-shifted descending triangle: for rows=5 you get 12345 / 2345 / 345 / 45 / 5 — each row starts at i and prints through rows.
The inner loop starts at j = i, not j = 1. When i increases, each row begins at that value and prints until rows.
Because each next row starts at a higher i, numbers below i are not printed — the triangle shifts left visually.
Program 1 counts the outer loop down and prints 1..i. Program 2 counts up and prints i..rows — same total prints, different shape.
cout << j stays on the same line. cout << "\n" ends the current line. Digits use cout << j; the row break uses cout << "\n" after the inner loop.
Yes. Use cout << j << " " instead of cout << j if you want spaced output.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total digit prints are n+(n-1)+…+1 = n(n+1)/2.

Did you know?

Each row starts at i and prints through rows, so the left edge shifts right each line — still O(n²) total prints for n rows.

Next: Reverse Descending Triangle

Continue with rows that count down from rows toward 1.

Program 3 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