Multiplication Grid
Completed
Read n and print an n×n multiplication table, each number right-aligned in 4 characters.
Input
One integer (1–12).
Output
n rows.
Example
Input
4
Output
1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16
Editor
Java
Hints
%4d pads a number to width 4.
Solution explanation
Formatting every cell to the same width lines the columns up.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int r = 1; r <= n; r++) {
StringBuilder row = new StringBuilder();
for (int c = 1; c <= n; c++) {
row.append(String.format("%4d", r * c));
}
System.out.println(row);
}
}
}