Multiplication Tables (Project)

For practice with flow control we are going to print out some multiplication tables.

First let's start by setting the number we will go up to in a global variable we will use as a constant.

TABLE_MAX = 12

Then we will use two nested for loops to iterate (aka repeat through each item) over the two numbers being multiplied. So the first loop is for the number on the left of the multiplication and the second loop is for the number on the right.

Let's start by just making the outer loop.

for x = 1, TABLE_SIZE do
    print("x is " .. x)
end

Now that we have the outer loop working, let's add the inner loop.

for x = 1, TABLE_SIZE do        
    print("x is " .. x)
    for y = 1, TABLE_SIZE do
        print("y is " .. y)
    end
end

And now let's actually print the multiplication results, though not yet in a table.

for x = 1, TABLE_SIZE do        
    for y = 1, TABLE_SIZE do
        print("x*y is " .. x*y)
    end
end

Now we can start on making things prettier.

Of course we would like a row heading and column headings for our tables. Add this to the top of your source file.

-- First print our row heading
io.write(string.rep(" ", 6)) -- print 6 spaces
for i = 1, TABLE_SIZE do -- repeat loop for each value of 1 from 1 to 12
    io.write(string.format("%4d", i)) -- print integer width 4
end
print() -- print newline
print(string.rep("-", 12*4+6)) -- print row of dashes

And update your loops so they look like this. Note that we are using io.write() instead of print() for outputting the numbers now. This allows us to leave off the newline character which moves things down one line until we want it so we can put all the numbers in a nice row.

Also, we are using string.format to print the number with a fixed format of 4 characters wide so that our items in our columns line up correctly.

for x = 1, TABLE_SIZE do
    -- Print this row's part of the column heading
    io.write(string.format("%3d  |", x))

    for y = 1, TABLE_SIZE do
        -- Print the value with a fixed width of 4 characters wide, padded with spaces
        -- so that numbers in the same column line up correctly.
        io.write(string.format("%4d", x*y))
    end
    print() -- print newline
end

Your final output should look like this:

         1   2   3   4   5   6   7   8   9  10  11  12
------------------------------------------------------
  1  |   1   2   3   4   5   6   7   8   9  10  11  12
  2  |   2   4   6   8  10  12  14  16  18  20  22  24
  3  |   3   6   9  12  15  18  21  24  27  30  33  36
  4  |   4   8  12  16  20  24  28  32  36  40  44  48
  5  |   5  10  15  20  25  30  35  40  45  50  55  60
  6  |   6  12  18  24  30  36  42  48  54  60  66  72
  7  |   7  14  21  28  35  42  49  56  63  70  77  84
  8  |   8  16  24  32  40  48  56  64  72  80  88  96
  9  |   9  18  27  36  45  54  63  72  81  90  99 108
 10  |  10  20  30  40  50  60  70  80  90 100 110 120
 11  |  11  22  33  44  55  66  77  88  99 110 121 132
 12  |  12  24  36  48  60  72  84  96 108 120 132 144