Exercise: Multiplication Table

Write a console program that:

  1. Asks the user to enter a single number from the user. This number specifies the number of rows and columns of the multiplication table. The number has to be between 1 and 10.
  2. Prints each row of the multiplication table.
  3. Uses the borders around numbers shown in the example outputs below.
Example program outputs (user input in green):
Number of rows and columns (between 1 and 10):
3
+---+---+---+
|  1|  2|  3|
+---+---+---+
|  2|  4|  6|
+---+---+---+
|  3|  6|  9|
+---+---+---+
Number of rows and columns (between 1 and 10):
10
+---+---+---+---+---+---+---+---+---+---+
|  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|
+---+---+---+---+---+---+---+---+---+---+
If the user enters a number that is not between 1 and 10, the program should print a message and ask the user to enter a new number:
Number of rows and columns (between 1 and 10):
0
0 is too low
Number of rows and columns (between 1 and 10):
21
21 is too high
Number of rows and columns (between 1 and 10):
1
+---+
|  1|
+---+

Hints 💡

General
printf
To make each cell in the table have the same width, you can use the printf method with a format string that specifies the width of the cell. For example, to print a number with a width of 3 characters, you can use the format string "%3d". If the number is shorter than 3 digits, the printf method will add spaces to the left of the number to make it 3 characters wide:
int product = outer * inner;
System.out.printf("%3d", product);
// Prints "  3" without quotes

Hand in instructions

  1. Make sure your program runs correctly.
  2. Hand in your program by uploading Main.java to Moodle.