PHP Program to Print Multiplication Table
Use a
for loop in PHP to print a multiplication table, for example: for ($i = 1; $i <= 10; $i++) { echo "$num x $i = " . ($num * $i) . "\n"; } prints the table for number $num.Examples
Input5
Output5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Input1
Output1 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
Input10
Output10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
How to Think About It
To print a multiplication table, think of a number and multiply it by numbers from 1 to 10. Use a loop to repeat the multiplication and print each result in a clear format.
Algorithm
1
Get the number for which to print the multiplication table.2
Start a loop from 1 to 10.3
Multiply the given number by the current loop number.4
Print the multiplication expression and result.5
Repeat until the loop ends.Code
php
<?php $num = 5; for ($i = 1; $i <= 10; $i++) { echo "$num x $i = " . ($num * $i) . "\n"; } ?>
Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Dry Run
Let's trace the multiplication table for number 5 through the code.
1
Set number
$num = 5
2
Start loop
$i = 1 to 10
3
Multiply and print
For $i=1: print '5 x 1 = 5' For $i=2: print '5 x 2 = 10' ... up to $i=10
| Iteration | Expression | Result |
|---|---|---|
| 1 | 5 x 1 | 5 |
| 2 | 5 x 2 | 10 |
| 3 | 5 x 3 | 15 |
| 4 | 5 x 4 | 20 |
| 5 | 5 x 5 | 25 |
| 6 | 5 x 6 | 30 |
| 7 | 5 x 7 | 35 |
| 8 | 5 x 8 | 40 |
| 9 | 5 x 9 | 45 |
| 10 | 5 x 10 | 50 |
Why This Works
Step 1: Loop from 1 to 10
The for loop repeats the multiplication for numbers 1 through 10.
Step 2: Multiply and print
Inside the loop, multiply the chosen number by the loop counter and print the result.
Step 3: Display format
The output shows the multiplication expression and its result clearly for each step.
Alternative Approaches
Using while loop
php
<?php $num = 5; $i = 1; while ($i <= 10) { echo "$num x $i = " . ($num * $i) . "\n"; $i++; } ?>
This uses a <code>while</code> loop instead of <code>for</code>, which is equally effective but less concise.
Using function with parameter
php
<?php function printTable($num) { for ($i = 1; $i <= 10; $i++) { echo "$num x $i = " . ($num * $i) . "\n"; } } printTable(5); ?>
Encapsulates the logic in a function for reuse with any number.
Complexity: O(1) time, O(1) space
Time Complexity
The loop runs exactly 10 times, so time is constant regardless of input.
Space Complexity
Only a few variables are used, so space is constant.
Which Approach is Fastest?
All approaches run in constant time; using a for loop is simplest and most readable.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop | O(1) | O(1) | Simple and clear repetition |
| While loop | O(1) | O(1) | Same as for loop but less concise |
| Function with parameter | O(1) | O(1) | Reusable code for any number |
Use a
for loop to repeat multiplication steps easily.Beginners often forget to increment the loop counter, causing an infinite loop.