-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path38. Print multiplication table .js
More file actions
75 lines (63 loc) · 1.75 KB
/
38. Print multiplication table .js
File metadata and controls
75 lines (63 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// print multiplication table :
// --------------------------------------------------------------------------------------------------------------::
const printTable1 = (num) => {
for (let i = 1; i <= 10; i++) {
console.log(`${num} x ${i} = ${num * i}`);
}
};
console.log(printTable1(4));
/*
EXPLANATION:
Basic Multiplication Table (1-10)
==========================================
const printTable1 = (num) => {
console.log(`Multiplication Table of ${num}:`);
console.log("=".repeat(25));
for (let i = 1; i <= 10; i++) {
console.log(`${num} x ${i} = ${num * i}`);
}
};
HOW IT WORKS:
- Simple for loop from 1 to 10
- Prints each multiplication in format: "num x i = result"
- Uses template literals for clean formatting
EXAMPLE: printTable1(5)
Output:
Multiplication Table of 5:
=========================
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
*/
// ------------------------------------------------------------------------------------
const printTable2 = (num, start, end) => {
for (let i = start; i <= end; i++) {
console.log(`${num} x ${i} = ${num * i}`);
}
};
console.log(printTable1(7));
/*
Method 2: Custom Range Multiplication Table
==========================================
const printTable2 = (num, start, end) => {
console.log(`Multiplication Table of ${num} (${start} to ${end}):`);
console.log("=".repeat(35));
for (let i = start; i <= end; i++) {
console.log(`${num} x ${i} = ${num * i}`);
}
};
HOW IT WORKS:
- Allows custom start and end points
- More flexible than fixed 1-10 range
- Useful for specific ranges like 5-15
EXAMPLE: printTable2(3, 5, 8)
Output:
Multiplication Table of 3 (5 to 8):
===================================
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
*/