English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Kotlin program multiplies two floating-point numbers

Comprehensive Collection of Kotlin Examples

In this program, you will learn how to multiply two floating-point numbers in Kotlin, store the result, and display it on the screen.

Example: Multiplication of two floating-point numbers

fun main(args: Array<String>) {
    val first = 1.5f
    val second = 2.0f
    val product = first * second
    println("Product: \$product")
}

When the program is run, the output is:

Product: 3.0

In the above program, we have two floating-point numbers1.5f and 2are stored in the variables first and second respectively.

Note that we use 'f' after the number. This ensures that the number is of Float type, otherwise it will be assigned as Double type.

You can also add :Float after the variable name (val first: Float) during declaration, but unlike Java, Kotlin will automatically perform this operation for you, so it is not necessary.

Then use * The operator multiplies first and second, and stores the result in a new float variable product.

Finally, use the println() function to print the result product on the screen.

This is the equivalent code in Java: In Java,Multiplication of Two Floating-Point Numbers

Comprehensive Collection of Kotlin Examples