Snippets

Multiply Two Numbers with Kotlin

Posted by I. B. Gd Pramana A. Putra, 12 Mar 22, last updated 17 Jul 22

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.

I love sharing code snippets as most of the time, a quick code example is what we're looking for instead of long-written articles. If you think my code snippets are helpful and save you a lot of time, please consider buying me a cup of coffee :)

Support me via · paypal · buymeacoffee · ko-fi · trakteer
Contributed Snippets
Answer & Responses
    No comments yet

Wanna write a response?

You have to login before write a comment to this post.