One of my friends came to me the other day asking I could help out with the logic for printing a multiplication table out. Go back to those days in second grade when you were learning all the combinations of small numbers from 1 to 12 for multiplication products. That was a long time ago. Quick, what’s 7 times 9?
Anyway, printing out a multiplication table like this one isn’t too hard.
x 1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
Let’s think of the logic behind this. In the first row, we have the X value, so it counts from 1 up to Xmax. In the first column, we have the Y value, so it counts from 1 up to Ymax. It is duly noted that the table only has a X listed, but that’s for simplicity, though there is a X and a Y, horizontal and vertical.
In the next row, where X is 1 and Y is 1, we have simple multiplication, 1 times 1, so the value is 1. In the second row and column we have the same numbers as the first row and column because of that by 1 multiplication. Now, we’ll take a look at some code.
public static String getNumber(int i, int j) {
int value = i * j;
if ( i == 0 ) {
value = (i+1) * j;
} else if ( j == 0 ) {
value = i * (j+1);
}
if ( i == 0 && j == 0 ) return "x";
else return value+"";
}
So let’s break this down. You have a value
which is obvious in its purpose: it’s the resultant multiplication of some row and some column. Then things get complicated. Since we’re, what? Either computer scientists or insane, we count from zero. Counting from zero makes us think about out logic.
If you’re in the first row, you’ll need to print out what would be the next row. What? Imagine you’re making a table for 5 by 5. You’ll need 1 2 3 4 5
. How do you get that? You multiply the column value by 1. But you only want this to happen naturally in the first row (which is actually the zeroth row by our zero-count). And we’ll need the same for the columns going down. So that’s what happens in that first if-else-if stack. If i
or j
is 0, then it will be temporarily incremented by 1 and it’ll print out the proper rows.
The the if-else stack at the end of this statement is pretty easy too. The if-statement checks to see if i == 0 && j == 0
, or if i and j are equal to zero, it will return the X that is the first value in the table. If that condition isn’t true, then it’ll print out either the regular i * j
value or the specialized first row/column value.
Now we’ll need a loop.
for (int i = 0; i <= height; i++) {
for (int j = 0; j <= width; j++) {
System.out.print( getNumber(i, j) + "\t" );
}
System.out.println("");
}
This is a nested for-loop. The outside loop will handle the rows (e.g. the height) and the inner loop will handle the columns (e.g. the width). There is a method call to the getNumber with arguments i and j, and a printout that has getNumber and appends a tab character at the end. Finally, in the outside loop, when the inner loop is done running, a newline is printed so that the next row will begin to form.
The logic for this type of program is an exercise in looping and conditional logic with index values. You can grab the source code here.
Happy multiplying.