This is a Kotlin code example or code snippet to multiply two numbers. This code contains some examples of how to create a simple program that multiplies two numbers and return the result.
Kotlin Code Example: Multiply Two Numbers
The following is an example of a Kotlin code to multiply two numbers. The user will be asked to input two numeric values, then store the values of those two numbers on their own variable, then multiply the two numbers and display the result.
In this two numbers multiplication with Kotlin, we will use the Arithmetic Operators in Kotlin. The arithmetic operator for multiplication in Kotlin is the * symbol.
import java.util.Scanner
fun main(args: Array<String>) {
val reader = Scanner(System.`in`)
print("Masukkan nilai bilangan pertama: ")
var firstVal:Int = reader.nextInt()
print("Masukkan nilai bilangan kedua: ")
var secVal:Int = reader.nextInt()
val result = firstVal * secVal
println("$firstVal + $secVal = $result")
}
Output
Let’s run/execute the Kotlin program code above, and enter the value for each of the numbers requested, for example, the number 8 and number 4, then the results are as follows:
Please input the value for firstVal variable: 8
Please input the value for secondVal variable: 4
8 * 4 = 32
Hopefully, this Kotlin code example and snippet program to multiply two numbers is useful.