C++ Multiplication Number Triangle Pattern

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

What Is This Pattern?

A triangular multiplication pattern prints row i with i products: i×1, i×2, … up to i×i. Row 4 is 4 8 12 16; row 5 ends with 25.

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

1
2 4
3 6 9
4 8 12 16
5 10 15 20 25     ← rows = 5

Unlike Program 48 (one value per line), this shape needs nested loops — the inner count grows with each row.

How to Solve It

Outer loop picks the row multiplier i. Inner loop prints i * j for j = 1..i.

MethodIdeaBest for
Growing triangleInner bound is j <= i — one more product each rowLearning, interviews, demos
Full table variantInner bound is j <= rows every rowSquare multiplication table (contrast)

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print (i * j) and a space
    print newline

Cheat sheet

GoalPattern
Outer rowsfor (i = 1; i <= rows; i++)
Inner columnsfor (j = 1; j <= i; j++)
Cell valuei * j (last on row = i * i)
Print cellcout << i * j << " "
End the rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << i * j << " "Stays on the same lineEach product
cout << "\n"Ends the current lineAfter the inner loop

Print cells 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 multiplication triangle updates instantly.

Use rows from 1 to 12. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 cells
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25

Worked Walkthrough

Trace row i = 4 — four products ending at i * i.

ji * jPrinted so far
14 × 1 = 44
24 × 2 = 84 8
34 × 3 = 124 8 12
44 × 4 = 164 8 12 16

After j finishes, cout << "\n" starts the next row. Every row ends with a perfect square: 1, 4, 9, 16, 25, …

C++ Programs

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

Example 1 — Fixed rows = 10

Hard-coded height — outer loop picks row i, inner loop prints i * j.

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

int main()
{
    int i, j;

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

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer loop picks the multiplier. i runs from 1 to 10 — one row per value of i.

2. Inner loop prints i products. j runs from 1 to i; each cell is i * j.

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 number of rows: ";
    cin >> rows;

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= i; j++)
            cout << i * 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 4 8 12 16.

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 — Compact rows = 5

Smaller height for quick tracing on paper or in interviews.

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

int main()
{
    int i, j;

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

        cout << "\n";
    }

    return 0;
}

How It Works

1. Only five rows. Easy to dry-run every i * j by hand.

2. Same formula. Nothing changes except the outer limit — proving the pattern scales.

3. Last value is always i * i. Row 5 ends with 25.

Edge Cases & Pitfalls

Check these before calling the solution done.

Newline

Do not put "\n" inside the inner loop

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

Inner bound

Use j <= i, not a fixed columns count

A fixed j <= rows every row builds a full rectangle table, not a triangle.

Spaces

Print a space after each product

Use cout << i * j << " " so columns stay readable.

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 i * j for j = 1..i.
  • Last cell: each row ends with i * i (a perfect square).
  • 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, print i*1 through i*i, then end the line.

Frequently Asked Questions

A triangle where row i contains i values: i*1, i*2, ... up to i*i. Row 4 prints 4 8 12 16.
Outer loop sets row i. Inner loop runs j = 1..i and prints i*j for each column.
The last value on row i is i*i. For i = 5, that is 25.
Program 48 is a 1D powers-of-11 sequence with one loop. Program 49 uses nested loops for a multiplication triangle.
Change the inner loop to j = 1..rows on every row instead of j = 1..i.
Printing a product (and space) stays on the same line. Printing a newline ends the current line. Cells 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 fixed-width formatting like setw(4) from <iomanip> so columns line up.

Did you know?

On row i, print i products: i*1, i*2, … up to i*i. Total prints = n(n+1)/2 — a triangular number, so time is O(n²).

Next: Mixed Number Triangle

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

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