JavaScript Multiplication Number Triangle Pattern

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

What Is This Pattern?

A triangular multiplication pattern prints the products i × 1, i × 2, …, i × i on row i.

Remember
Rule: on row i, append 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 (1D powers of 11), this uses nested loops — total products for n rows is n(n+1)/2.

How to Solve It

One nested loop: row multiplier outside, column factors inside. End each row with console.log.

MethodIdeaBest for
Basic triangleAppend i * j for j = 1..iClassic demos
Aligned columnsSame products with padStart(3)Neater multi-digit output

Pseudocode

Pseudocode
for i from 1 to rows:
    line = ""
    for j from 1 to i:
        append i * j and a space
    print line

Cheat sheet

GoalPattern
Outer (rows)for (let i = 1; i <= rows; i++)
Inner (cols)for (let j = 1; j <= i; j++)
Productline += (i * j) + " ";
Alignedline += String(i * j).padStart(3, " ");
End the rowconsole.log(line);
Total productsrows * (rows + 1) / 2

Printing Numbers vs Starting a New Line

APIEffectUse for
line += …Stays on the same rowEach product i * j
console.log(line)Ends the current rowAfter j = 1..i finish

Append without a newline, then end the row once.

Live Preview

Change the row count and the multiplication triangle updates instantly.

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

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

Worked Walkthrough

Trace three rows — watch how j runs to i and each cell is i * j.

Row ij valuesProductsPrinted row
1111
21, 22, 42 4
31, 2, 33, 6, 93 6 9

Last value on row i is always i * i. Total products for n rows = n(n+1)/2.

JavaScript Programs

Three complete programs: fixed rows = 5, prompt input, and padStart alignment. Use View Output for samples, or Try It Yourself to edit and run live.

Example 1 — Fixed rows = 5

Nested loops append i * j with a trailing space.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += (i * j) + " ";
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Outer loop sets the multiplier. i is both the row index and the left factor in each product.

2. Inner loop grows with the row. j runs from 1 to i; each cell appends i * j.

3. End the row. console.log(line) after the inner loop starts the next row.

Example 2 — User Input Rows

Read the row count with prompt and validate before drawing.

JavaScript
const rows = parseInt(prompt("Enter number of rows:"), 10);

if (!Number.isFinite(rows) || rows < 1) {
  console.log("Please enter a positive integer.");
} else {
  for (let i = 1; i <= rows; i++) {
    let line = "";
    for (let j = 1; j <= i; j++) {
      line += (i * j) + " ";
    }
    console.log(line);
  }
}
Try It Yourself

How It Works

1. Prompt and validate. Use parseInt and require rows >= 1.

2. Same triangle core. Only the source of rows changes — i * j matches Example 1.

3. Entering 4. Ten products total (4×5/2), ending at 16.

Example 3 — Aligned Columns

Same products; padStart(3) keeps columns neat as values grow.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let line = "";
  for (let j = 1; j <= i; j++) {
    line += String(i * j).padStart(3, " ");
  }
  console.log(line);
}
Try It Yourself

How It Works

1. Same geometry. Still i * j for j = 1..i.

2. Fixed width. padStart(3, " ") reserves three columns per product (like C# {0,3}).

3. Cleaner for larger n. Two-digit values no longer shift the columns.

Edge Cases & Pitfalls

Check these before calling the solution done.

log early

Call console.log only after the row is built

Logging inside the inner loop prints one product per line.

j vs i

j * i vs i * j

Same products — fine. Just stay consistent; both equal i * j.

rows = 1

Single-row case

Output is just 1 — one product on one line.

Bad input

Validate parseInt

Check Number.isFinite(rows) and require rows >= 1 before the loops.

Time and Space Complexity

ProgramTimeExtra space
Fixed / aligned (Examples 1, 3)O(n²)O(n) per row string
User input (Example 2)O(n²)O(n) per row string

Products appended are 1 + 2 + … + n = n(n+1)/2 — quadratic in n.

Key Takeaways

  • Rule: on row i, append i * j for j = 1..i.
  • Last cell: always i * i (e.g. 25 on row 5).
  • Break the row: call console.log(line) only after the column loop.
  • Complexity: O(n²) — total products = n(n+1)/2.

One line: nest two loops, append i * j for each column up to i, then log the row.

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 appends i*j for each column.
The last value on row i is i*i. For i = 5, that is 25.
line += appends products on the same row. console.log(line) ends the row after j = 1..i finish.
Program 48 is a 1D powers-of-11 sequence with one loop. Program 49 uses nested loops for a multiplication triangle.
Use String(i*j).padStart(3, " ") so columns line up — see Example 3.
O(n²) for n rows because total appends are 1+2+…+n = n(n+1)/2.
Change the inner loop to j = 1..rows on every row: for (let j = 1; j <= rows; j++).

Did you know?

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

Next: Rotating Number Pattern

Continue with the next pattern in the JavaScript number-pattern series.

Program 50 tutorial →

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