Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

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 = 60Every 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.
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 = 80A 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 = 90A 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 = 84Plain 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 = 120To 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| Variant | Loop | Input | Best for |
|---|---|---|---|
| Scanner + for loop | Single for | User input | Table for any number entered at runtime |
| Fixed for loop | Single for | Hard-coded | Quick demos and learning loop mechanics |
| While loop | Single while | Either | When the stop condition is not a simple counter |
| Custom limit (N) | Single for | User input | Tables that run beyond 10 |
| printf formatting | Single for | Either | Neatly aligned, column-style output |
| Nested loops | Two for | None | Full 10 by 10 grid of all tables |
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.
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.
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.
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.
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.
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.
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.
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