C++ Ascending Number Triangle Pattern

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

What Is This Pattern?

An ascending number triangle grows one digit per row: row i prints 1 through i with no spaces between digits. With rows = 5 you get 1, 12, 123, 1234, 12345.

Remember
Rule: on row i, print j for j = 1..i

1
12
123
1234
12345     ← rows = 5

Unlike Program 4 (descending rows..i shrink), this shape counts the outer loop up so each row is longer than the last.

How to Solve It

Outer loop picks row i from 1 to rows. Inner loop prints digits 1..i.

MethodIdeaBest for
Tight digitscout << j with no spacesClassic textbook demos
Spaced digitscout << j << " "Easier reading when digits get wider

Pseudocode

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

Cheat sheet

GoalPattern
Outer rowsfor (i = 1; i <= rows; i++)
Inner digitsfor (j = 1; j <= i; j++)
Print digitcout << j;
Spaced formcout << j << " ";
End the rowcout << "\n";

Printing Numbers vs Starting a New Line

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

Print digits without a newline, then end the row once. Putting "\n" inside the inner loop turns the triangle into a vertical list.

Live Preview

Change the row count and the ascending triangle updates instantly.

Use rows from 1 to 9 (single digits stay tidy). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 digits
1
12
123
1234
12345

Worked Walkthrough

Trace row i = 4 — four digits glued together as 1234.

jPrintLine so far
111
2212
33123
441234

After j finishes, cout << "\n" starts the next row. Row 5 will print one more digit — 12345.

C++ Programs

Three complete programs: fixed rows = 5, cin input, and spaced digits. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — outer loop grows the row, inner loop prints 1..i.

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

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

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= i; j++)
            cout << j;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer loop grows the row. i runs from 1 to 5 — one more digit each line.

2. Inner loop prints 1..i. j runs from 1 to i; each digit uses cout << j with no space.

3. Newline after the row. cout << "\n" runs only after the inner loop finishes.

Example 2 — User Input Rows

Read the row count at runtime so the triangle grows or shrinks with input.

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

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

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= i; j++)
            cout << j;

        cout << "\n";
    }

    return 0;
}

How It Works

1. Same nested-loop core. Only the source of rows changes from a literal to cin.

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

3. Validate in real apps. Prefer checking cin failure and requiring rows >= 1 (tip below).

Safer input tip
if (!(cin >> rows) || rows < 1)
{
    cout << "Please enter a positive integer.\n";
    return 1;
}

Example 3 — Spaced Digits

Same loops — add a space after each digit for easier reading.

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

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

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

        cout << "\n";
    }

    return 0;
}

How It Works

1. Only the print changes. cout << j << " " instead of cout << j.

2. Loop bounds stay the same. Outer and inner limits match Example 1 exactly.

3. Useful for wider values. Spaces keep columns readable when digits become two characters.

Edge Cases & Pitfalls

Check these before calling the solution done.

Newline

Do not put "\n" inside the inner loop

That prints one digit per line. Call cout << "\n" only after the inner loop.

Outer direction

Count up, not down

Outer i = rows..1 with the same inner loop is Program 1 (shrinking). This pattern uses i = 1..rows.

Missing newline

Omitting the row break

Without cout << "\n", every digit glues onto one endless line.

Bad cin

Validate input

Check cin >> rows and require rows >= 1 before the loops.

Time and Space Complexity

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

Total prints are 1 + 2 + … + n = n(n + 1) / 2 — quadratic in n. Only a few integers of extra memory.

Key Takeaways

  • Rule: on row i, print j for j = 1..i.
  • Grow up: outer i = 1..rows makes each row one digit longer.
  • Break the row: call cout << "\n" only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each row i from 1 to rows, print digits 1..i, then end the line.

Frequently Asked Questions

The outer loop runs i from 1 to rows. For each row i, the inner loop runs j from 1 to i and prints j. Row 1 prints 1, row 2 prints 12, and so on until row rows prints 1..rows.
Because the outer loop counts up and the inner bound equals i. When i increases, each row prints one more digit than the previous row.
Program 4 prints rows..i in descending order (54321, 5432, ...). Program 5 prints 1..i in ascending order — a growing triangle instead of a shrinking one.
Program 1 counts the outer loop down and prints 12345, 1234, ... Program 5 counts up and prints 1, 12, 123, ... — same inner loop, opposite outer direction.
Printing a digit stays on the same line. Printing a newline ends the current row. Digits use cout without a newline; the row break uses cout << "\n" after the inner loop.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Use cout << j << " " instead of cout << j — see Example 3.
After cin >> rows, check failure and require rows >= 1 before the loops.

Did you know?

Row i prints digits 1 through i. The outer loop counts up from 1 to rows, so each row grows by one digit — total prints = n(n+1)/2, time O(n²).

Next: Reverse Ascending Triangle

Continue with the next pattern in the C++ number-pattern series.

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