Exercise: Multiplication Tables

Exercise 10 Very easy

Prerequisites for the exercise

  1. PHP Random Numbers
  2. All previous chapters

Objective

Create a multiplication table for a randomly-chosen positive integer less than or equal to 100.

Description

Time to go random!

In this exercise, you have to randomly come up with a positive integer less than or equal to 100, and then create a multiplication table for that integer starting at 1 and going all the way up to 12.

For instance, if the integer is 5, you'll compute 5 x 1, 5 x 2, ..., all the way upto 5 x 12.

For the output, start with the following line:

Multiplication table for <integer>:

where <integer> is the randomly-chosen integer.

Next up, comes the multiplication table. Leave a blank line before printing the multiplication table.

Each row of the table is to be presented on a new line in the following form:

<integer> x <i> = <product_i>

where <integer> is the randomly-chosen integer, <i> is the multiplier (starting at 1, and incrementing with each new row) and <product_i> is the product of these two numbers.

Shown below is an example to clarify all these outputs:

Multiplication table for 4: 4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36 4 x 10 = 40 4 x 11 = 44 4 x 12 = 48
View Solution

New file

Inside the directory you created for this course on PHP, create a new folder called Exercise-10-Multiplication-Tables and put the .php solution files for this exercise within it.

Solution

We'll start by computing the random integer. Let's call it $n.

The random integer has to be positive (i.e. above 0) and less than or equal to 100. That is, it should be in the range 1 – 100. To generate a random number in this range, we'll call rand(1, 100).

Let's get done with this first followed by making the first output:

<?php

$n = rand(1, 100);

echo 'Multiplication table for ', $n, ":\n";

The next step is to print a blank line, so let's also get this done:

<?php

$n = rand(1, 100);

echo 'Multiplication table for ', $n, ":\n";

// Blank line.
echo "\n";

With this done, now we only have to print the multiplication table for the random integer. One way is to copy paste 12 lines of echo, but as you'd agree, that's very repetitive, and totally against the DRY (Don't Repeat Yourself) principle.

The best option is to use a loop to repetitively execute print().

Which loop? for or while?

Well, for is useful to iterate a specific number of times or go over a sequence, whereas while is more suited to some condition-based iteration.

Definitely, we'll go with for since we have to repeat a known number of times — 12 specifically.

The general form of each row in the multiplication table is given in the exercise's description above, and based on that we'll format the output:

<?php

$n = rand(1, 100);

echo 'Multiplication table for ', $n, ":\n";

// Blank line.
echo "\n";

for ($i = 1; $i <= 12; $i++) {
   echo $n, ' x ', $i, ' = ', $n * $i, "\n";
}

This completes our exercise.

Code on GitHub

"I created Codeguage to save you from falling into the same learning conundrums that I fell into."

— Bilal Adnan, Founder of Codeguage