Kotlin program to print a multiplication table:
In this post, we will learn how to print a multiplication table in Kotlin. The program will take one number as input from the user and print the multiplication table i.e. the table of multiplication from 1 to that number.
Kotlin program:
Below is the complete kotlin program:
package com.company
fun main() {
println("Enter a number : ")
val givenNumber = Integer.valueOf(readLine())
for (i in 1..10) {
println("$givenNumber * $i = " + (givenNumber * i))
}
}
In this program,
- we are reading a number as input from the user and storing it in givenNumber
- Using a for loop, we are printing the multiplication table.
- Inside the loop, it is printing the multiplication table.
It will print output as like below:
Enter a number :
4
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
You might also like:
- 5 different Kotlin program to iterate through a mutablelist
- 5 different ways in Kotlin to find a string in a list of strings
- How to sort a kotlin map by value
- Binary search implementation in Kotlin for a list of custom objects
- 6 ways to add items to a list in Kotlin
- Different ways to write to a file in Kotlin