Increasing Sum Number Triangle (0-Based) in C++

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print an increasing sum triangle in C++ that starts from 0 and grows row by row.

The idea is simple: for each cell, print i + j where i is the current row index and j is the column index.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10
1

Complete C++ Program

This version prints 6 lines (from i = 0 to 5). Each line prints values computed as i + j.

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

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

    for (i = 0; i <= maxRow; i++) {
        for (j = 0; j <= i; j++) {
            if (j > 0) cout << ' ';
            cout << (i + j);
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Set the last row index

int maxRow = 5; prints rows from 0 through 5 (total 6 lines).

Setup
2

Outer loop (0-based rows)

for (i = 0; i <= maxRow; i++) controls which row is being printed.

Row control
3

Inner loop (0..i values)

for (j = 0; j <= i; j++) prints exactly i + 1 values on row i.

Column control
4

Compute the sum

Printing i + j makes each row start at i and increase by 1 as j increases.

Value rule
=

Sum-based triangle

Total printed values are triangular (6 rows here): 1+2+…+n, so time complexity is O(n²).

2

Variation — User Input Version

Let the user decide the last row index (maximum row value) using cin:

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

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

    cout << "Enter the last row index (e.g., 5): ";
    cin >> maxRow;

    for (i = 0; i <= maxRow; i++) {
        for (j = 0; j <= i; j++) {
            if (j > 0) cout << ' ';
            cout << (i + j);
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Change the value rule to create new patterns (e.g., i*j, i-j, or i*i + j)
  • Format numbers with fixed width using setw() for alignment
  • Convert to 1-based output by printing i + j + 1 instead
  • Make it right-aligned by printing leading spaces before each row
  • Print only even sums by checking (i+j)%2==0

Avoid

  • Hard-coding the size in multiple places (store it in one variable)
  • Printing trailing spaces if exact formatting matters (use a separator rule)
  • Forgetting the newline after each row
  • Using endl inside loops (it flushes output and slows printing)

Key Takeaways

1

Starting the loops from 0 makes the first printed value 0.

2

The number of values on row i is i+1 because j runs from 0 to i.

3

The value rule i + j increases by 1 as you move right.

4

Total work is quadratic in the number of rows: O(n²).

❓ Frequently Asked Questions

Because the first row uses i = 0, and the inner loop runs only once with j = 0. The value is i + j = 0.
Print i + j + 1 instead of i + j. The output becomes 1, 2 3, 3 4 5, ...
Yes. If you want exactly rows lines, loop i from 0 to rows-1. That prints 1 line when rows=1, 2 lines when rows=2, etc.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Explore More C++ Number Patterns!

Try changing the value rule to create dozens of pattern variations with the same loop structure.

All Number Patterns →
Did you know?

The formula i + j creates constant-sum diagonals in a 2D grid. That’s why it naturally forms neat increasing sequences in triangles and matrices.

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