Multiplication Number Triangle in Java

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops i×j

What You’ll Learn

The multiplication triangle prints row i as i×1, i×2, …, i×i — e.g. 1, 2 4, 3 6 9. This tutorial covers nested-loop logic, live preview, worked Java examples, edge cases, and O(n²) complexity.

Shape Rule

i × j

Row i prints products i×1 through i×i.

Nested Loops

i = 1..rows

Outer loop picks row i; inner loop runs j = 1..i.

Row Products

print(i*j)

Each cell is i * j — standard int multiplication for lab sizes.

Table Link

Times tables

Each row shows the first i multiples of i.

Live Preview

3–12 rows

Pick a row count and draw the multiplication triangle instantly in the browser.

O(n²)

Complexity

Total prints ≈ n(n+1)/2 — quadratic time; extra memory stays O(1).

Introduction

A multiplication number triangle pattern builds row i with products i×1, i×2, …, i×i. Each row has one more value than the row above.

In Java you use nested loops: outer for (i = 1; i <= rows; i++), inner for (j = 1; j <= i; j++), print i*j, then println() after each row.

Why it matters?

It is a classic nested-loop exercise that connects pattern printing with multiplication tables.

Key Highlights

Outer loop i

i = 1..rows picks each row number.

Inner loop j

j = 1..i prints i×j each row.

Nested Loops

Outer row + inner products — classic two-loop pattern.

Series Foundation

Follow Program 48 powers of 11; continue to Program 50 decreasing-increasing pattern.

In short: outer i=1..rows, inner j=1..i, print i*j, then println().

📝 Problem & Approach

Given rows = 10, print ten lines: 1, 2 4, 3 6 9, … up to row 10.

Java
// rows = 4 (conceptual output)
// 1
// 2 4
// 3 6 9
// 4 8 12 16

Inputs & Outputs

ItemTypeDescription
rowsintHow many lines to print (typically ≥ 1).
i, jintRow index i and column index j; cell value is i*j.
Printed outputtexti values on row i — triangle shape.

Minimal workflow

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print i * j
    newline

Approach comparison

ApproachIdeaBest for
Nested i×jprint(i * j) inside inner loopStandard triangle — inner bound j<=i
Scanner inputsc.nextInt() for rowsUser-chosen row count
No trailing spacePrint space only before 2nd+ valuesCleaner row formatting — Example 3

⚡ Quick Reference

GoalPattern
Set rowsint rows = 10;
Outer loopfor (i = 1; i <= rows; i++)
Inner loopfor (j = 1; j <= i; j++)
Print cellSystem.out.print((i * j) + " ");
Row breakSystem.out.println(); after inner loop
Program 48 contrastPowers of 11 uses one loop; this pattern uses nested loops with i×j products

📋 Outer Loop vs Inner Loop vs Combined

How outer row selection, inner products, and row breaks work together.

Outer loop
for (i = 1; i <= rows; i++)

Picks row number i — triangle height.

Inner loop
for (j = 1; j <= i; j++)

Prints exactly i products on row i.

Cell value
i * j

Each value is the product of row and column indices.

Learning tip
trace i=4

Dry-run row 4: j=1..4 → prints 4 8 12 16.

Context

When This Pattern Shows Up

Reach for this pattern when teaching nested loops, multiplication tables, and growing inner bounds.

  1. First lab exercise

    Classic follow-up after single-loop series patterns like powers of 11.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with Scanner for a flexible row count.

  4. Gateway to variants

    Compare with Program 48 (powers of 11), then continue to Program 50 (decreasing-increasing pattern).

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in loop variables, running totals, and O(n²) thinking.

🔮 Live Preview

Choose a row count and draw the multiplication triangle pattern in the browser.

Try 3, 5, or 10 rows (up to 12).

Live result
Press "Draw pattern".

Examples Gallery

Three complete Java programs — fixed rows = 10, Scanner input, and a no-trailing-space variant. Click View Output to reveal sample console results.

📚 Getting Started

Print ten rows with nested loops and i*j products.

Example 1 — Fixed rows = 10

Hard-coded size — outer loop for rows, inner loop prints i*j.

Java
public class MultiplicationNumberTriangle {
    public static void main(String[] args) {
        int rows = 10;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print((i * j) + " ");
            }
            System.out.println();
        }
    }
}

How It Works

When i = 3, the inner loop runs j = 1, 2, 3 and prints 3 6 9. Each row has exactly i values.

📈 Practical Variant

Read rows with Scanner for flexible output size.

Example 2 — Scanner Input

Same nested loops; row count comes from user input.

Java
import java.util.Scanner;

public class MultiplicationNumberTriangleInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int rows = sc.nextInt();

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print((i * j) + " ");
            }
            System.out.println();
        }

        sc.close();
    }
}

How It Works

Identical loop structure to Example 1; only the row count is dynamic.

⚡ Formatting Variant

Avoid trailing spaces by printing a space only before the second and later values.

Example 3 — No Trailing Space

Print a leading space only when j > 1.

Java
public class MultiplicationNumberTriangleNoTrail {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                if (j > 1) System.out.print(" ");
                System.out.print(i * j);
            }
            System.out.println();
        }
    }
}

How It Works

Same i*j logic; spacing is cleaner without a trailing space after the last value on each row.

🧠 How the Algorithm Prints Rows

1

Set up

System.out is built in; use Scanner when reading input. Set rows (e.g. 10).

Setup
2

Loop rows

for (i = 1; i <= rows; i++) — one iteration per output line.

Loop
3

Inner loop products

for (j = 1; j <= i; j++) prints i*j with spaces, building each row.

Inner
4

New line after row

System.out.println(); after the inner loop moves to the next row.

Break
=

Multiplication number triangle pattern complete

Total prints ≈ n(n+1)/2O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — row i = 4

Trace row 4 to see how the inner loop builds 4 8 12 16.

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

After the inner loop, println() moves to the next row.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: use Scanner for dynamic row count — see Example 2.

2. Multiplication Table Link

Each row shows the first i multiples of i — direct link to times tables.

Example: compare row 5 (5 10 15 20 25) with the 5-times table.

3. Console Formatting Drills

Practice println vs print for multi-line vs single-line output.

Example: put System.out.print(res + " ") for one-line output — Example 3.

4. Full Table Variant

Change inner bound to j <= rows to print a rectangular multiplication table.

Example: print rows=5 with j<=rows and compare triangle vs table shape.

5. Complexity Intuition

One loop iteration per row makes O(n²) concrete for beginners.

Example: count values for rows=5 → 1+2+3+4+5 = 15 printed numbers.

6. Input Validation Labs

Pair the pattern with Scanner and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner loop roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner Java courses.

  1. 1. Instant Visual Feedback

    Wrong inner bound (j <= rows on every row) breaks the triangle shape.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Change rows, use Scanner, or remove trailing spaces with conditional print.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: learn the nested loops first; then try Scanner input and the no-trailing-space variant in Example 3.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Name Variables Clearly

    Use rows for height and i/j for row/column indices.

  2. 2. Prefer Scanner

    Avoid crashes when the user types letters instead of a number.

  3. 3. Row Break After Inner Loop

    Call println() after the inner loop finishes — not inside it.

  4. 4. Use print for Values

    Use print(i*j + " ") inside the inner loop; one println() per row.

  5. 5. Dry-Run One Small n

    Trace rows = 4, i = 3, j = 2 on paper before coding larger demos.

Pro Tip: if each row has the same number of values, check whether the inner loop ends at rows instead of i.

Common Pitfalls

Mistakes that commonly break multiplication number triangle patterns.

  1. 1. println() Inside the Inner Loop

    Each product lands on its own line — you get a column, not a triangle row.

    → Use print inside the inner loop; println() only after it finishes.

  2. 2. Wrong Inner Bound (j <= rows)

    Every row prints rows values — you get a rectangle, not a triangle.

    → Keep for (j = 1; j <= i; j++) for the triangle shape.

  3. 3. Forgetting the Row Break

    Omitting println() after the inner loop glues all rows onto one line.

    → Always call System.out.println() after the inner loop completes.

  4. 4. Swapping i and j in the Product

    Using j*i is fine mathematically, but confusing bounds break the intended row layout.

    → Keep outer i for rows and inner j from 1 to i.

  5. 5. Unchecked Scanner Input

    Letters or empty input throw InputMismatchException.

    → Use sc.hasNextInt() before sc.nextInt().

  6. 6. Hard-coding 10 Everywhere

    Using literal 10 in loop bounds instead of variable rows breaks dynamic input.

    → Use one rows variable for the outer loop bound.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single line

Output is one line: 1.

rows = 0

Empty pattern

Loop never runs — print nothing or show a message.

Negative

rows < 0

Treat as invalid; re-prompt instead of silent empty output.

Large n

Many rows

Large row counts produce many values — fine for labs; use smaller n for quick demos.

Bad input

Non-numeric Scanner input

Unchecked Scanner leaves rows unset — call sc.hasNextInt() first.

Compact

Single-line form

Use Print space only before 2nd+ values for one horizontal line — see Example 3.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Change rows

  • Try rows = 3, 6, or 12
  • Count values: 1+2+…+n

2. Full table variant

  • Change inner loop to j <= rows
  • Compare rectangular table vs triangle

3. Addition triangle

  • Replace i*j with i+j
  • Observe the new sequence

4. Next in series

  • Continue with Program 50 decreasing-increasing pattern
  • Connect to symmetric row patterns

Notes

  • Value count. Total prints ≈ n(n+1)/2 (e.g. 15 values for rows=5).
  • print stays on the line; println advances — mix them carefully.
  • Validate rows > 0 for interactive programs; rows = 1 prints one value.
  • Inner loop must use j <= i for the triangle — not a fixed width.

Quick Takeaway: outer i=1..rows, inner j=1..i, print(i*j), then println().

⏱️ Time and Space Complexity

ProgramTimeExtra space
Fixed rows = 10 (Example 1)O(n²)O(1)
Scanner input (Example 2)O(n²)O(1)
No trailing space (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

The multiplication triangle combines nested loops with i×j products — a natural step after powers-of-11 patterns. Master the fixed-rows version first, then try Scanner input and the no-trailing-space variant in Example 3.

Practice the three examples above, then continue to Program 50 for the decreasing-increasing number pattern (12345, 21234…).

Keep println() after the inner loop — one row break per outer iteration.

💡 Best Practices

✅ Do

  • Explain outer i, inner j=1..i, and i*j before coding
  • Use print(i*j + " ") and println() after the inner loop
  • Validate rows ≥ 1 for interactive programs
  • Check Scanner return value before using rows
  • State O(n²) time when asked about complexity

❌ Don’t

  • Put println() inside the inner loop (one value per line)
  • Use inner bound j <= rows when triangle shape is required (j <= i)
  • Hard-code 5 instead of variable rows
  • Ignore bad console input in user-facing demos
  • Skip the rows = 1 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this multiplication number triangle pattern

Print the pattern the beginner-friendly way.

5
Core concepts
02

Outer loop

i = 1..rows

Code
03

Inner j=1..i

print(i * j)

Logic
n 04

Growing rows

n(n+1)/2 prints

I/O
O 05

Complexity

O(n²)

Analysis

❓ Frequently Asked Questions

The inner loop runs from j=1 to j=i. As i increases, each row prints one extra value.
Each printed number is the product i×j. Row 4 prints 4×1, 4×2, 4×3, 4×4 → 4 8 12 16.
For i=4, the inner loop uses j=1..4, so you print 4×1, 4×2, 4×3, 4×4.
Store the row count in rows and use for (int i=1; i<=rows; i++) in the outer loop.
Print a space before numbers starting from the second value, or build the row with StringBuilder — see Example 3.
Yes. Change the inner loop to for (int j=1; j<=rows; j++) so every row has rows columns.
O(n²) for n rows. Total prints are 1+2+...+n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.

Did you Know? 🔊

Row i prints i×1, i×2, …, i×i. Each row has one more value than the row above — a classic nested-loop exercise linked to multiplication tables.

Continue to Program 50

Move on to the decreasing-increasing number pattern in the Java 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