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.
Approach
How to Solve It
Outer loop picks the row multiplier i. Inner loop prints i * j for j = 1..i.
Method
Idea
Best for
Growing triangle
Inner bound is j <= i — one more product each row
Learning, interviews, demos
Full table variant
Inner bound is j <= rows every row
Square 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
Goal
Pattern
Outer rows
for (i = 1; i <= rows; i++)
Inner columns
for (j = 1; j <= i; j++)
Cell value
i * j (last on row = i * i)
Print cell
cout << i * j << " "
End the row
cout << "\n";
Printing Numbers vs Starting a New Line
API
Effect
Use for
cout << i * j << " "
Stays on the same line
Each product
cout << "\n"
Ends the current line
After 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.
Try it
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 result5 rows · 15 cells
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
Trace
Worked Walkthrough
Trace row i = 4 — four products ending at i * i.
j
i * j
Printed so far
1
4 × 1 = 4
4
2
4 × 2 = 8
4 8
3
4 × 3 = 12
4 8 12
4
4 × 4 = 16
4 8 12 16
After j finishes, cout << "\n" starts the next row. Every row ends with a perfect square: 1, 4, 9, 16, 25, …
Code
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;
}
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;
}
Output (when user enters 4)
Enter number of rows: 4
1
2 4
3 6 9
4 8 12 16
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;
}
Output
1
2 4
3 6 9
4 8 12 16
5 10 15 20 25
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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²).