Write a java program to compute multiplication table?
Write a Java program to compute the Multiplication table.
Write a Java program to compute multiplication tables. Consider the following multiplication table, which we denote it by M:
Multiplication table function: M[i][j] = i*j
where 1<= (i,j) <=n
Run the program and test your program with some testcases.

Java Program
import java.util.InputMismatchException;
import java.util.Scanner;
public class MultiplicationTable {
public static void main(String[] args) {
int n;
Scanner input = null;
try {
input = new Scanner(System.in);
System.out.println("Enter size of the array:");
n = input.nextInt();
if (n > 0) {
int[][] a = computeTable(n);
printTable(a);
} else {
System.out.println("Enter positive number.");
System.exit(0);
}
} catch (InputMismatchException ime) {
System.out.println("Not a valid input");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null) {
input.close();
}
}
}
static int[][] computeTable(int n) {
int[][] a = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
a[i][j] = (i+1)*(j+1);
return a;
}
static void printTable(int[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a.length; j++)
printWithSpace(a[i][j],10);
System.out.println();
}
}
static void printWithSpace(int n, int z) {
String s = "" + n, space = " ";
int l = s.length();
System.out.print(space.substring(0, z-l) + s);
}
}
Run output
TestCase: Happy paths
Enter size of the array:
5
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Run output 2
Enter size of the array:
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

Testcase : Unhappy paths
Enter size of the array:
0
Enter positive number.
Another run
Enter size of the array:
table
Not a valid input