Hero Background

Next-Gen App & Browser Testing Cloud

Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Next-Gen App & Browser Testing Cloud

Write a Java program for printing the Multiplication table?

To print a multiplication table in Java, read an integer from the user with the Scanner class, then run a for loop from 1 to 10. On each pass, multiply the entered number by the loop counter and print the result. The program below prints the standard ten-row table for any number you supply.

import java.util.Scanner;

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

        for (int i = 1; i <= 10; i++) {
            System.out.println(num + " * " + i + " = " + (num * i));
        }
        sc.close();
    }
}

Output

Enter any number: 6
6 * 1 = 6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60

How the Multiplication Table Program Works

Every version of this program follows the same four-step idea. Once you understand the steps, you can swap loop types or output formats without changing the underlying logic.

  • Get the number: read the value to multiply, either from the user with Scanner or as a hard-coded variable in the source.
  • Loop a fixed number of times: iterate the counter from 1 to 10 (or to a custom limit) so each row of the table is produced once.
  • Multiply: compute num * i on each pass, where i is the current counter value.
  • Print: output a formatted line such as num * i = product, then move to the next iteration.

Print the Table Using a For Loop (Fixed Number)

If you do not need runtime input, assign the number directly in code. This keeps the example self-contained and is the cleanest way to learn the loop mechanics. The Java Program to Demonstrate for Loop declares the counter, condition, and increment together, which is ideal for a known 1 to 10 range.

public class MultiplicationTable {
    public static void main(String[] args) {
        int num = 8;

        for (int i = 1; i <= 10; i++) {
            System.out.println(num + " * " + i + " = " + (num * i));
        }
    }
}

Output

8 * 1 = 8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80

Print the Table Using a While Loop

A Java Program to Demonstrate While Loop produces exactly the same table. The difference is purely structural: you initialize the counter before the loop and increment it inside the body. Forgetting that increment is the most common cause of an infinite loop here.

public class MultiplicationTable {
    public static void main(String[] args) {
        int num = 9;
        int i = 1;

        while (i <= 10) {
            System.out.println(num + " * " + i + " = " + (num * i));
            i++;
        }
    }
}

Output

9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

Print the Table Up to a Custom Limit (N)

A multiplication table does not have to stop at 10. Read both the number and the upper limit, then loop up to that limit. This is useful when you want, say, a table of 7 extended all the way to 12.

import java.util.Scanner;

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

        for (int i = 1; i <= limit; i++) {
            System.out.println(num + " * " + i + " = " + (num * i));
        }
        sc.close();
    }
}

Output

Enter the number: 7
Enter the limit: 12
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
7 * 11 = 77
7 * 12 = 84

Formatted Output With printf (Aligned Columns)

Plain string concatenation leaves ragged columns once the products grow past one digit. Using System.out.printf with width specifiers right-aligns each value in a fixed-width field. Here %2d reserves two spaces for the operands, %3d reserves three for the product, and %n inserts a portable newline.

public class MultiplicationTable {
    public static void main(String[] args) {
        int num = 12;

        for (int i = 1; i <= 10; i++) {
            System.out.printf("%2d x %2d = %3d%n", num, i, num * i);
        }
    }
}

Output

12 x  1 =  12
12 x  2 =  24
12 x  3 =  36
12 x  4 =  48
12 x  5 =  60
12 x  6 =  72
12 x  7 =  84
12 x  8 =  96
12 x  9 = 108
12 x 10 = 120

Full 1 to 10 Multiplication Grid With Nested Loops

To print the complete 10 by 10 times table, use two nested loops. The outer loop controls the rows, the inner loop controls the columns, and printf("%4d", i * j) keeps every column aligned. After each inner loop finishes, System.out.println() moves to the next row.

public class MultiplicationTable {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            for (int j = 1; j <= 10; j++) {
                System.out.printf("%4d", i * j);
            }
            System.out.println();
        }
    }
}

Output

   1   2   3   4   5   6   7   8   9  10
   2   4   6   8  10  12  14  16  18  20
   3   6   9  12  15  18  21  24  27  30
   4   8  12  16  20  24  28  32  36  40
   5  10  15  20  25  30  35  40  45  50
   6  12  18  24  30  36  42  48  54  60
   7  14  21  28  35  42  49  56  63  70
   8  16  24  32  40  48  56  64  72  80
   9  18  27  36  45  54  63  72  81  90
  10  20  30  40  50  60  70  80  90 100

Variants at a Glance

VariantLoopInputBest for
Scanner + for loopSingle forUser inputTable for any number entered at runtime
Fixed for loopSingle forHard-codedQuick demos and learning loop mechanics
While loopSingle whileEitherWhen the stop condition is not a simple counter
Custom limit (N)Single forUser inputTables that run beyond 10
printf formattingSingle forEitherNeatly aligned, column-style output
Nested loopsTwo forNoneFull 10 by 10 grid of all tables

Common Mistakes to Avoid

  • Missing the Scanner import: any program that reads input needs import java.util.Scanner; at the top, or it will not compile.
  • Off-by-one loop bounds: use i <= 10 to include the row for 10. Writing i < 10 stops the table at row 9.
  • Forgetting i++ in a while loop: if you never increment the counter, the condition stays true and the program loops forever.
  • Mixing newline styles: prefer %n inside printf for a portable line break instead of hard-coding \n, which can render inconsistently across platforms.
  • Operator precedence in prints: wrap the multiplication in parentheses, like (num * i), so it is computed before the surrounding string concatenation.

Small console programs like this are a natural first step into Java. When those programs grow into real test automation, you can run your Java and Selenium suites across thousands of browser and OS combinations using TestMu AI Selenium Automation on the cloud grid, without maintaining local infrastructure.

Frequently Asked Questions

How do you print a multiplication table in Java?

Read an integer with the Scanner class, then run a for loop from 1 to 10. Inside the loop, multiply the number by the loop counter and print the result. This produces the standard ten-row multiplication table for the number you entered.

Can I print a multiplication table without Scanner input?

Yes. Assign the number to a variable directly in code, for example int num = 6, and loop from 1 to 10. The Scanner class is only needed when you want the number to come from user input at runtime.

How do I print the full 1 to 10 multiplication grid in Java?

Use two nested loops. The outer loop runs from 1 to 10 and controls each row, the inner loop runs from 1 to 10 and controls each column, and you print i*j inside the inner loop. Use printf with a width specifier such as %4d so the columns stay aligned.

Should I use a for loop or a while loop for a multiplication table?

Both produce identical output. A for loop is preferred because the counter, condition, and increment are declared together, which makes a fixed 1 to 10 range easy to read. A while loop is equally valid and is handy when the stopping condition is not a simple counter.

How do I align the multiplication table output in columns?

Use System.out.printf with width specifiers instead of plain concatenation. For example, printf("%2d x %2d = %3d%n", num, i, num*i) right-aligns each number in a fixed-width field so the equals signs and products line up neatly.

Why does my Java multiplication table only print nine rows?

That is an off-by-one error in the loop condition. Use i <= 10 rather than i < 10 if you want rows 1 through 10. With i < 10 the loop stops after i equals 9, so the row for 10 is never printed.

Related Questions

Test Your Website on 3000+ Browsers

Get 100 minutes of automation test minutes FREE!!

Test Now...

KaneAI - Testing Assistant

World’s first AI-Native E2E testing agent.

...

TestMu AI forEnterprise

Get access to solutions built on Enterprise
grade security, privacy, & compliance

  • Advanced access controls
  • Advanced data retention rules
  • Advanced Local Testing
  • Premium Support options
  • Early access to beta features
  • Private Slack Channel
  • Unlimited Manual Accessibility DevTools Tests