Kotlin Program to Multiply two Floating Point Numbers

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

Example: Multiply Two Floating Point Numbers

  1. fun main(args: Array<String>) {
  2. val first = 1.5f
  3. val second = 2.0f
  4. val product = first * second
  5. println("The product is: $product")
  6. }

When you run the program, the output will be:

The product is: 3.0

In the above program, we have two floating point numbers 1.5f and 2.0f stored in variables first and second respectively.

Notice, we have used f after the numbers. This ensures the numbers are Float, otherwise they will be assigned – type Double.

You can also add :Float after variable name (val first: Float) during declaration, but, unlike Java, Kotlin automatically does that for you so it is not mandatory.

first and second are then multiplied using the * operator and the result is stored in a new float variable product.

Finally, the result product is printed on the screen using println() function.