Multiplication Triangle Pattern in C++

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

What You’ll Learn

How to print a multiplication triangle in C++ where row i prints:

i*1, i*2, ..., i*i.

This is a simple and practical nested-loop pattern (similar to multiplication tables).

⭐ Pattern Output

For rows = 10, the pattern looks like this:

Output
1\n2 4\n3 6 9\n4 8 12 16\n5 10 15 20 25\n6 12 18 24 30 36\n7 14 21 28 35 42 49\n8 16 24 32 40 48 56 64\n9 18 27 36 45 54 63 72 81\n10 20 30 40 50 60 70 80 90 100
1

Complete C++ Program

The outer loop controls the row i. The inner loop prints products j*i from j=1 to j=i.

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

int main() {
    int i, j;

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

    return 0;
}

🧠 How It Works

1

Choose the number of rows

Here we loop up to 10, so the triangle has 10 rows.

Setup
2

Outer loop picks i

For each i, we create one row of products.

Row control
3

Inner loop multiplies j*i

The inner loop runs j = 1..i and prints j*i.

Multiplication
4

New line after each row

cout << "\\n"; moves printing to the next line.

Line break
=

Multiplication triangle

Total values printed are 1+2+…+n, so runtime is O(n²) for n rows.

2

Variation — User Input Version

Let the user choose the number of rows at runtime. This version also aligns columns using setw(4).

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

int main() {
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;

    if (rows <= 0) return 0;

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print the full multiplication table by making the inner loop run to rows
  • Change the formula to print squares (i*i) or cubes (i*i*i)
  • Remove trailing spaces by printing separators conditionally
  • Align output using setw() for larger row counts
  • Validate input (check cin.fail())

Avoid

  • Using very large rows without formatting (output becomes hard to read)
  • Forgetting the newline after each row
  • Hard-coding 10 when you want a reusable program
  • Using endl inside loops (unnecessary flushing)

Key Takeaways

1

Outer loop controls the row number i.

2

Inner loop prints products i*j from j=1..i.

3

Total printed values are n(n+1)/2.

4

Overall runtime is O(n²) for n rows.

❓ Frequently Asked Questions

Because it prints 4*1, 4*2, 4*3, 4*4.
Yes. You can start i from another value or offset i and j to shift the multiplication.
Run the inner loop from j=1 to rows for every row, not just to i.
O(n²) because the total prints are 1+2+…+n.

Explore More C++ Number Patterns!

Multiplication-based patterns are a great bridge between math tables and nested loops.

All Number Patterns →
Did you know?

This triangle is essentially the lower-left portion of a multiplication table. If you print all columns, you get the full table.

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