C++ Number Triangle Pattern (Starting from 0)

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

What Is This Pattern?

An increasing number triangle from 0 uses zero-based loops: row i prints i + 1 values of i + j — so the first cell is 0.

Remember
Rule: for i from 0 to max
        for j from 0 to i
          print (i + j) and a space

0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10     ← max = 5

Follows the triangle from 1 in Program 33; next is the right-aligned continuous counter in Program 35.

How to Solve It

Outer loop runs i = 0..max. Inner loop prints j = 0..i with the formula i + j, separated by spaces.

MethodIdeaBest for
Formula i + jWhen j = 0, value equals i — row starts at its indexLearning, interviews, exams
Compare with Program 33Same shape; zero-based loops replace i + j - 1Seeing how indexing changes the start

Pseudocode

Pseudocode
for i from 0 to max:
    for j from 0 to i:
        print (i + j) and a space
    print newline

Cheat sheet

GoalPattern
Grow each rowfor (i = 0; i <= max; i++)
Print i+1 valuesfor (j = 0; j <= i; j++)
Start at 0cout << i + j << " ";
Row starts at iWhen j = 0, i + j = i
End of rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << value << " "Stays on the same lineEach number on the row
cout << "\n"Ends the current lineAfter the inner loop finishes

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

Live Preview

Change the max i and the zero-based triangle updates instantly — capped at 9 for readable demos.

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

Live result max = 5 · 21 numbers
0 
1 2 
2 3 4 
3 4 5 6 
4 5 6 7 8 
5 6 7 8 9 10 

Worked Walkthrough — max = 5

Trace how each (i, j) pair maps to i + j.

iValuesCountPrints
0010
11, 221 2
22, 3, 432 3 4
33..643 4 5 6
44..854 5 6 7 8
55..1065 6 7 8 9 10

Total numbers = 1 + 2 + … + (max+1) = (max+1)(max+2)/2 → O(n²).

C++ Programs

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

Example 1 — Fixed max = 5

Hard-coded max — zero-based loops print i + j with a trailing space.

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

int main()
{
    int i, j;

    for (i = 0; i <= 5; i++)
    {
        for (j = 0; j <= i; j++)
            cout << i + j << " ";

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer grows from 0. Row i prints exactly i + 1 numbers.

2. Formula fills the cells. i + j makes row i start at i and count upward.

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

Example 2 — User Input Max

Read max with cin, validate, then use the same formula core.

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

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

    cout << "Enter max i: ";
    if (!(cin >> max) || max < 0)
    {
        cout << "Please enter a non-negative integer.\n";
        return 1;
    }

    for (i = 0; i <= max; i++)
    {
        for (j = 0; j <= i; j++)
            cout << i + j << " ";

        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and negative values before printing.

2. Same core. i + j matches Example 1 — only max comes from the user.

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

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

Example 3 — Compact max = 2

Same structure with only three rows — easy to confirm zero-based indexing on paper.

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

int main()
{
    int max = 2;
    int i, j;

    for (i = 0; i <= max; i++)
    {
        for (j = 0; j <= i; j++)
            cout << i + j << " ";

        cout << "\n";
    }

    return 0;
}

How It Works

1. Three rows. i = 0 → 0; i = 1 → 1 2; i = 2 → 2 3 4.

2. Trace on paper. If you start at i = 1, the leading 0 disappears and the shape matches Program 33 less cleanly.

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

Edge Cases & Pitfalls

Check these before calling the solution done.

i = 1

Missing zero row

Starting at i = 1 or using i + j - 1 drops the leading 0. Keep i = 0..max with i + j.

j <= max

Rectangle instead of triangle

Inner bound j <= max makes every row the same width. Use j <= i.

no space

Glued numbers

Without << " ", values run together (12). Always print a trailing space.

\n inside

Broken rows

If cout << "\n" sits inside the inner loop, you get one number per line. Call it only after the loop.

max = 0

Single value

Output is just 0. A good sanity check for input validation.

cin

Check the stream

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

Time and Space Complexity

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

Row i prints i + 1 numbers. Total = 1 + 2 + … + (n+1) = (n+1)(n+2)/2 for max = n → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Formula: each cell is i + j — first cell is 0 when both loops start at 0.
  • Zero-based width: inner loop runs j = 0..i so row i has i + 1 numbers.
  • Break the row: values with spaces in the inner loop; cout << "\n" after it finishes.
  • Complexity: O(n²) time from (n+1)(n+2)/2 prints; O(1) extra space.

One line: for each i from 0 to max, print i + j for j = 0..i with spaces, then newline.

Frequently Asked Questions

An increasing triangle from 0: for max=5 you get 0 / 1 2 / 2 3 4 / 3 4 5 6 / 4 5 6 7 8 / 5 6 7 8 9 10 — each value is i + j with zero-based loops.
Because the loops start at i = 0 and j = 0, so i + j = 0.
j increases from 0 to i, so i + j increases by 1 each step — producing consecutive numbers.
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
Program 34 uses the formula i + j per cell. Program 35 is a right-aligned continuous counter triangle.
cout << i + j << " " keeps values separated on the same row. cout << "\n" ends the row.
After prompting, use if (!(cin >> max) || max < 0) to reject bad input before printing.
O(n²) for max = n because total prints are 1 + 2 + … + (n+1) = (n+1)(n+2)/2.

Did you know?

Each printed value is computed as i + j. With i = 0 and j = 0 the first row prints 0; row i = 2 prints 2, 3, 4 — a zero-based left-shifted increasing triangle.

Next: Right-Aligned Continuous Counter

Continue with a right-aligned triangle that counts continuously across rows.

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