Python Programming: Producing a Multiplication Table

Python Programming: Producing a Multiplication Table

People learning to program often struggle with how to decompose a problem into the steps necessary to write a program to solve that problem. This is one of a series of posts in which I take a problem and go through my decision-making process that leads to a program.

The problem: produce a multiplication table. I assume that at this point you know about statements, conditionals, loops, and functions.

You can find a video with more details at https://youtu.be/QJqfYn_4pmw.

Design

Think of design as being similar to planning a trip. Given a specific destination, how do I get there from where I am now? I can better understand what I need to do by working a small example by hand.

An n x n multiplication table where n = 4 looks like this:

      1    2    3    4  
   --------------------
 1 |  1    2    3    4  
 2 |  2    4    6    8  
 3 |  3    6    9   12  
 4 |  4    8   12   16 

To produce this, I took each row number in the left-hand column and multiplied by the row of numbers across the top. So the first row of products is

    1 x (1, 2, 3, 4) = (1, 2, 3, 4)

the second row is

    2 x (1, 2, 3, 4) = (2, 4, 6, 8)

and so forth. This may seem obvious given how young you probably were when you were first exposed to multiplication tables, but by doing this I am trying to understand my decision-making process while producing the table. I can see that I repeat the process of multiplying a number by the numbers 1 to n. The number than I multiply by each row of numbers is the row number itself-row 1, row 2, and so forth.

I can see that I have a two processes that repeat and therefore I will probably need two loops. One loop produces the integers from 1 to n with each being multiplied by the row number. The other loop produces the row numbers, which themselves are the integers from 1 to n. The loop that produces the row numbers will be the outer loop, with the other loop being inside of it.

At this point I have the function

def multTable(n) :
    for r in range(1, n+1) :
        for c in range(1, n+1) :
            print("%3d  " % (r*c), end="")

        print()

that produces

  1    2    3    4  
  2    4    6    8  
  3    6    9   12  
  4    8   12   16

This is my starting point. Now I want to add the row and column numbers that make it look like a traditional multiplication table.f What should I do first? Since printing the column values will require indenting the output with spaces, I want to do that after I see how much space the row numbers require.

Before printing a row of output value, that is, the products, I need to print the row number followed by the vertical bar. By default the print() function includes a newline, so I suppress it using end=””. Then the row of products will follow the line number. At this point my function looks like this:

def multTable(n) :
    for r in range(1, n+1) :
        print("%2d |" % r, end="")
        for c in range(1, n+1) :
            print("%3d  " % (r*c), end="")

        print()

and it produces

 1 |  1    2    3    4  
 2 |  2    4    6    8  
 3 |  3    6    9   12  
 4 |  4    8   12   16  

Now I am ready to add the column numbers and a row of dashes to form a line. For this, I decided to create a separate function:

def printHeader(n) :
    print("    ", end="")

    for c in range(1, n+1) :
        print("%3d  " % c, end="")

    print()

    print("   ", end="")

    for c in range(1, n+1) :
        print("-----", end="")

    print()

Much of the logic in printHeader() is similar to what I wrote in multTable(). Could I have put the logic of printHeader() inside multTable() instead of it’s own function? Sure, but I think this makes the underlying logic easier to follow. It also has the benefit that I can “turn off” the header if I wish by simply commenting out the function call.

Here is the final program:

def printHeader(n) :
    print("    ", end="")

    for c in range(1, n+1) :
        print("%3d  " % c, end="")

    print()

    print("   ", end="")

    for c in range(1, n+1) :
        print("-----", end="")

    print()


def multTable(n) :
    printHeader(n)

    for r in range(1, n+1) :
        print("%2d |" % r, end="")
        for c in range(1, n+1) :
            print("%3d  " % (r*c), end="")

        print()

#######  main  #######
n = int( input("enter size (15 rows max):  ") )

multTable(n)

One thought on “Python Programming: Producing a Multiplication Table

Leave a Reply

Your email address will not be published. Required fields are marked *