Display Multiplication Table in Java

What you’ll learn

  • How a loop prints times-table rows one by one.
  • How to format rows like n x i = result.
  • Fixed and user-input versions in Java.

Prerequisites

Basic Java loops, methods, and integer arithmetic.

  • You know how a for loop prints repeated rows.
  • You understand multiplication and string output formatting.

The idea

A multiplication table is a sequence of products: for a base n, print n x i for i = 1 to 10.

Live preview

Live result
Press "Print table".

Algorithm

Read base number

Use a fixed value or input from the user.

Loop 1 to 10

For each i, compute base * i.

Print row

Output in form base x i = result.

📜 Pseudocode

Pseudocode
for i from 1 to 10:
    print n, i, n*i
1

Table for 5 (fixed)

java
public class Main {
    static void printMultiplicationTable(int base, int last) {
        System.out.println("Multiplication table for " + base + ":");
        for (int i = 1; i <= last; i++) {
            System.out.println(base + " x " + i + " = " + (base * i));
        }
    }

    public static void main(String[] args) {
        printMultiplicationTable(5, 10);
    }
}
2

Table for typed input

java
import java.util.Scanner;

public class Main {
    static void printMultiplicationTable(int base, int last) {
        System.out.println("Multiplication table for " + base + ":");
        for (int i = 1; i <= last; i++) {
            System.out.println(base + " x " + i + " = " + (base * i));
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a positive integer: ");
        int n = sc.nextInt();
        if (n <= 0) {
            System.out.println("Please enter a positive integer.");
            return;
        }
        printMultiplicationTable(n, 10);
    }
}

Optimization notes

Keep it simple. Prefer clear loops and method extraction before micro-optimizations.

Reuse computed values. Avoid recomputing the same sub-result inside nested loops.

❓ FAQ

It is a list of products like n×1, n×2, ... up to a limit such as 10.
Only the multiplier changes each row, so a loop avoids repeating code.
Yes. Change the loop upper limit to 12 or any positive value.
Yes. while and for can both print a multiplication table.
Math still works, but many beginner exercises accept only positive values.
If you print k rows, time is O(k) and extra space is O(1).

🔄 Input / output examples

Use the sample programs as reference: provide valid numeric input and verify the output pattern matches the problem definition.

Edge cases

Bounds

Smallest valid input

Confirm behavior for minimum accepted values.

Validation

Invalid input

Guard against non-numeric or out-of-range values when input is user-provided.

⏱️ Time and space complexity

O(k) time for k printed rows, O(1) extra space.

Summary

  • Use a direct Java implementation of the numeric rule.
  • Validate input and test boundary cases.
  • Discuss complexity clearly in interviews.
Did you know?

A multiplication table for n is the list n×1, n×2, .... A loop prints each line quickly and clearly.

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.

8 people found this page helpful