## Apple Interview Question

Staff Engineers**Country:**United States

```
/* print n*n multiplication table, and format output based on input */
public static void printTable(int n){
int highest = n*n;
int tens = 1;
highest /= 10;
while (highest != 0){
tens++;
highest /= 10;
}
String format = "%" + tens + "s ";
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.printf(format, i*j);
}
System.out.println();
}
}
```

Since we know the maximum result can be three digits i.e 12 * 12 = 144, we can use the string formatting function when printing our output to correctly align the output. In this case, we have 3 digits as our max length, so we can format it in that particular way.

Solution:

Test code:

- prudent_programmer March 17, 2018