Multiplication Triangle Pattern in C++

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:
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 100Complete C++ Program
The outer loop controls the row i. The inner loop prints products j*i from j=1 to j=i.
#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
Choose the number of rows
Here we loop up to 10, so the triangle has 10 rows.
Outer loop picks i
For each i, we create one row of products.
Inner loop multiplies j*i
The inner loop runs j = 1..i and prints j*i.
New line after each row
cout << "\\n"; moves printing to the next line.
Multiplication triangle
Total values printed are 1+2+…+n, so runtime is O(n²) for n rows.
Variation — User Input Version
Let the user choose the number of rows at runtime. This version also aligns columns using setw(4).
#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
rowswithout formatting (output becomes hard to read) - Forgetting the newline after each row
- Hard-coding 10 when you want a reusable program
- Using
endlinside loops (unnecessary flushing)
Key Takeaways
Outer loop controls the row number i.
Inner loop prints products i*j from j=1..i.
Total printed values are n(n+1)/2.
Overall runtime is O(n²) for n rows.
❓ Frequently Asked Questions
4*1, 4*2, 4*3, 4*4.i from another value or offset i and j to shift the multiplication.j=1 to rows for every row, not just to i.Explore More C++ Number Patterns!
Multiplication-based patterns are a great bridge between math tables and nested loops.
This triangle is essentially the lower-left portion of a multiplication table. If you print all columns, you get the full table.
12 people found this page helpful
