Kotlin Program to Print Multiplication Table
You can print a multiplication table in Kotlin using a
for loop like this: for (i in 1..10) { println("$number x $i = ${number * i}") } where number is the base number.Examples
Inputnumber = 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
Inputnumber = 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
Inputnumber = 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 multiplying the given number by each number from 1 to 10. Use a loop to repeat this multiplication and print the result each time. This way, you get all the products in order.
Algorithm
1
Get the number for which to print the multiplication table2
Start a loop from 1 to 103
In each loop iteration, multiply the number by the loop counter4
Print the multiplication expression and result5
End the loop after 10 iterationsCode
kotlin
fun main() {
val number = 7
for (i in 1..10) {
println("$number x $i = ${number * 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 number 3 through the code
1
Set number
number = 3
2
Start loop
i = 1
3
Calculate and print
3 x 1 = 3
4
Next iteration
i = 2, print 3 x 2 = 6
5
Continue until i=10
Print 3 x 10 = 30
| i | Output |
|---|---|
| 1 | 3 x 1 = 3 |
| 2 | 3 x 2 = 6 |
| 3 | 3 x 3 = 9 |
| 4 | 3 x 4 = 12 |
| 5 | 3 x 5 = 15 |
| 6 | 3 x 6 = 18 |
| 7 | 3 x 7 = 21 |
| 8 | 3 x 8 = 24 |
| 9 | 3 x 9 = 27 |
| 10 | 3 x 10 = 30 |
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
In each loop, multiply the base number by the current loop number and print the formatted string.
Step 3: Output all lines
This prints all lines of the multiplication table in order, showing the calculation and result.
Alternative Approaches
Using while loop
kotlin
fun main() {
val number = 4
var i = 1
while (i <= 10) {
println("$number x $i = ${number * i}")
i++
}
}This uses a <code>while</code> loop instead of <code>for</code>. It works the same but requires manual increment.
Using repeat function
kotlin
fun main() {
val number = 6
repeat(10) { i ->
println("$number x ${i + 1} = ${number * (i + 1)}")
}
}The <code>repeat</code> function runs the block 10 times with index <code>i</code> starting at 0.
Complexity: O(1) time, O(1) space
Time Complexity
The loop runs exactly 10 times, so time is constant O(1) regardless of input.
Space Complexity
No extra memory is used except for loop variables, so space is O(1).
Which Approach is Fastest?
All approaches run in constant time and space; choice depends on readability and style preference.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loop | O(1) | O(1) | Simple and clear iteration |
| While loop | O(1) | O(1) | Manual control of loop variable |
| Repeat function | O(1) | O(1) | Concise syntax with index |
Use string templates like
"$number x $i = ${number * i}" for clear and simple output formatting.Beginners often forget to increment the loop counter or use the wrong range, causing infinite loops or missing lines.