A loop that runs a fixed number of times is the easiest way to print a table.
What to build
A program that prints the multiplication table of a number.
Requirements
- Read n.
- Print 10 lines in the form
n x i = resultfor i from 1 to 10.
Input
One integer n.
Output
10 lines.
Constraints
-100 ≤ n ≤ 100.
Example
Input:
7
Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
Why it works
The loop counter i takes each value from 1 to 10 and the product is calculated in each round.
Examples
Example 1 · Input
7
Expected output
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
Example 2 · Input
1
Expected output
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9 1 x 10 = 10
Hints
Hint 1
Use a for loop from 1 to 10.
Hint 2
Print each line as "$n x $i = " followed by the product.
Solution
Show one correct solution (try it yourself first)
<?php
$n = (int) trim(fgets(STDIN));
for ($i = 1; $i <= 10; $i++) {
echo "$n x $i = ", $n * $i, "\n";
}