0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Print Multiplication Table

Use a for loop in JavaScript to print a multiplication table, for example: for(let i=1; i<=10; i++){ console.log(`${num} x ${i} = ${num*i}`); } where num is the number for which you want the table.
📋

Examples

Inputnum = 2
Output2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20
Inputnum = 5
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
Inputnum = 0
Output0 x 1 = 0 0 x 2 = 0 0 x 3 = 0 0 x 4 = 0 0 x 5 = 0 0 x 6 = 0 0 x 7 = 0 0 x 8 = 0 0 x 9 = 0 0 x 10 = 0
🧠

How to Think About It

To print a multiplication table, think of counting from 1 to 10 and multiplying each count by the chosen number. For each step, show the multiplication expression and its result. This repeats until you reach 10 times the number.
📐

Algorithm

1
Get the number for which to print the multiplication table.
2
Start a loop from 1 to 10.
3
Multiply the number by the current loop count.
4
Print the multiplication expression and result.
5
Repeat until the loop reaches 10.
💻

Code

javascript
const num = 7;
for (let i = 1; i <= 10; i++) {
  console.log(`${num} x ${i} = ${num * i}`);
}
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
🔍

Dry Run

Let's trace printing the multiplication table for 3 through the code.

1

Set number

num = 3

2

Start loop

i = 1

3

Calculate and print

Print '3 x 1 = 3'

4

Increment loop

i = 2

5

Calculate and print

Print '3 x 2 = 6'

iExpressionOutput
13 x 13
23 x 26
33 x 39
43 x 412
53 x 515
63 x 618
73 x 721
83 x 824
93 x 927
103 x 1030
💡

Why This Works

Step 1: Loop from 1 to 10

The for loop runs 10 times, once for each multiplier from 1 to 10.

Step 2: Multiply and print

Inside the loop, multiply the chosen number by the loop counter and print the result with a clear format.

Step 3: Repeat for all multipliers

This repeats until all multiplication steps from 1 to 10 are printed.

🔄

Alternative Approaches

Using while loop
javascript
const num = 4;
let i = 1;
while(i <= 10) {
  console.log(`${num} x ${i} = ${num * i}`);
  i++;
}
This uses a <code>while</code> loop instead of <code>for</code>, which some find easier to read but requires manual increment.
Using Array.from and forEach
javascript
const num = 6;
Array.from({length: 10}, (_, i) => i + 1).forEach(i => {
  console.log(`${num} x ${i} = ${num * i}`);
});
This uses modern array methods for a functional style, which is concise but may be less clear for beginners.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs exactly 10 times, so time complexity is O(n) where n=10, which is constant time in practice.

Space Complexity

No extra space is used besides a few variables, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time for 10 iterations; differences are mostly readability and style.

ApproachTimeSpaceBest For
For loopO(n)O(1)Simple and clear for beginners
While loopO(n)O(1)When manual control of loop needed
Array.from with forEachO(n)O(1)Functional style and concise code
💡
Use template literals with backticks for easy and readable string formatting in JavaScript.
⚠️
Beginners often forget to increment the loop counter, causing an infinite loop.