Multiplication Table in Java
Print a multiplication table in Java with Scanner and a for loop from 1 to 10. Each line calculates n * i; large input products may need a wider type.
1import java.util.Scanner;23public class Main {4 public static void main(String[] args) {5 Scanner sc = new Scanner(System.in);6 System.out.print("Enter a number: ");7 int n = sc.nextInt();89 for (int i = 1; i <= 10; i++) {10 System.out.println(n + " x " + i + " = " + (n * i));11 }12 sc.close();13 }14}
Sample Output
Enter a number: 5 5 x 1 = 5 5 x 2 = 10 ... 5 x 10 = 50
Each line keeps n fixed at 5 and increases i by one. The full output includes all ten products, not an ellipsis.
In this one
- Reuse one input across ten calculations
- Combine labels with a product
- Distinguish displayed x from operator *
How it works
- 1
Read n.
- 2
Start i at 1.
- 3
Print n, i, and n * i.
- 4
Increase i until all ten lines have printed.
Edge cases
Zero table
Enter 0
Every product is zero, but all ten lines still print.
Negative table
Enter -3
The products are -3, -6, and so on through -30.
What's going on
Display the multiplication table of an entered whole number from times one through times ten. This is a familiar maths task that makes a useful loop exercise because the operation stays the same while one factor changes.
Scanner is the keyboard reader. It reads n once, then the loop changes i from 1 to 10. The print statement joins labels, n, i, and the calculated product. Parentheses around n * i make it clear that this part is a calculation rather than text to display.
For n = 5, the lines run from 5 x 1 = 5 through 5 x 10 = 50. System.out.println gives every multiplication its own line. The x in the message is only text; Java multiplication uses *. A zero or negative n still has a table. Very large products need a wider type than this small int example.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is the last product for n = 5?
Pick an answer to check it.
Question 2
Which symbol multiplies in Java code?
Pick an answer to check it.
Questions people ask
Why is x in quotes?
It is part of the human-readable message. The actual calculation uses *.
How many lines are in the table?
Ten calculation lines, plus the input prompt.
Can I print up to twelve instead?
Yes. Change the loop limit from 10 to 12.