Loops – multiplication table

We will create two example programs to demonstrate use of loops to generate multiplication tables.

Example 1: Multiplication table with used-specified multiplier

In the first program, we will ask the user for the multiplier and generate the multiplication table for that value from 0 to 10, which is defined as a macro MULT_LIMIT . Notice, that the loop condition uses  <=  – this means that the ending value is inclusive, which is typically not what we would want, but in this case we do.

Algorithm for the task

In the diagram, notice the control flows – when using loops, the control flow returns to an earlier point. It’s also important to have the condition correctly formed and located. In this case, we are using an entry-controlled loop.

Example 2: Complete multiplication table

In the second example, we will take a look at nested loops and produce a full multiplication table from 1 to 10.

Notice, that contrary to usual, the starting value of the counters is 1 and the ending value is again inclusive. Also notice the location of the line change and padding the answers with spaces! Think how and when the  printf()  statements are being used!