Java Multiplication Number Triangle Pattern

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

What Is This Pattern?

A multiplication number triangle prints products i × j on each row — row i shows i×1, i×2, …, i×i.

Remember
Rule: for i = 1..rows
        for j = 1..i: print i * j

1
2 4
3 6 9
4 8 12 16     ← rows = 4

Unlike Program 48 (one value per line, ×11), here nested loops build a growing triangle from the times table.

How to Solve It

Outer loop picks the row; inner loop prints i * j from j = 1 to j = i.

MethodIdeaBest for
Fixed rowsHard-code rows; print i*jLabs and demos
Scanner inputSame loops; read rows at runtimeInteractive practice
No trailing spaceSpace before 2nd+ values onlyCleaner row formatting

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print i * j
    new line

Cheat sheet

GoalPattern
Set sizeint rows = 10;
Walk rowsfor (int i = 1; i <= rows; i++)
Walk columnsfor (int j = 1; j <= i; j++)
Print cellSystem.out.print((i * j) + " ");
End rowSystem.out.println();

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach i*j value in a row
System.out.printlnEnds the lineAfter the inner loop finishes

Print values side by side, then break the line once per row.

Live Preview

Change the row count and the multiplication triangle updates instantly.

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

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

Worked Walkthrough — rows = 4

Trace each row: j runs from 1 to i, and every cell is i * j.

ij rangePrinted row
11..11
21..22 4
31..33 6 9
41..44 8 12 16

Row 3 is the 3-times table cut short at 3×3 — not the full table through 10.

Java Programs

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

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

1. Outer loop. i is the row number and the left factor in every product.

2. Inner loop. j runs to i, so row i has exactly i values.

3. Print and break. print((i * j) + " ") builds the row; println() starts the next line.

Example 2 — Scanner Input

Read rows at runtime. Same nested-loop logic.

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();
        if (rows < 1) return;

        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

1. Prompt and guard. Read rows; exit early if it is less than 1.

2. Same loops. Only the source of rows changes — the i*j logic is identical.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive integer.");
    return;
}
int rows = sc.nextInt();
if (rows < 1) {
    System.out.println("Enter a positive integer.");
    return;
}

Example 3 — No Trailing Space

Same i * j logic — space only before the second and later values on each row.

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

1. Same products. Still print i * j for j = 1..i.

2. Smarter spacing. if (j > 1) adds a space before later values — no trailing space after the last number.

3. Same shape. The triangle looks identical; only the end-of-row whitespace is cleaner.

Edge Cases & Pitfalls

Check these before calling the solution done.

Missing println

One long line

Without println() after the inner loop, every value prints on a single horizontal line.

j <= rows

Full table

Using j <= rows instead of j <= i prints a square multiplication table, not a triangle.

rows = 1

Single value

Output is just 1.

rows = 0

Empty output

The outer loop never runs. Guard interactive input with rows >= 1.

Wrong product

Print j only

Printing j alone gives 1, 1 2, 1 2 3 — not the multiplication triangle.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive integer.

Time and Space Complexity

ProgramTimeExtra space
All three examplesO(n²)O(1)

Total printed values for n rows: 1 + 2 + … + n = n(n+1)/2.

Key Takeaways

  • Rule: outer i=1..rows, inner j=1..i, print i*j.
  • Shape: each row is one longer — linked to the i-times table.
  • Spacing: use print for cells and one println per row.
  • Complexity: O(n²) prints for n rows.

One line: for each row i, print i×1 … i×i, then start a new line.

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.

Next: Decreasing-Increasing Number Pattern

Continue with 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