Kotlin Program to Multiply Two Floating-Point Numbers


In this article, we will understand how to multiply two floating-point numbers. A floating-point number, is a positive or negative whole number with a decimal point. There are two types of floating point in Kotlin

  • Float
  • Double

Below is a demonstration of the same −

Suppose our input is

val1: 10.5
val2: 45.0

The desired output would be

The product is: 472.5

Algorithm

  • Step 1 − Start

  • Step 2 − Declare three floating points: val1, val2 and product

  • Step 3 − Define the floating-point values

  • Step 4 − Read the values

  • Step 5 − Multiply the two values using a multiplication operator (*)

  • Step 6 − Display the result

  • Step 7 − Stop

Example 1

In this example, we will multiply two floating-point numbers using the multiplication arithmetic operator

fun main() { val val1 = 10.5f val val2 = 45.0f println("The first value is defined as: $val1") println("The second value is defined as: $val2") val product = val1 * val2 println("The product is: $product") }

Output

The first value is defined as: 10.5
The second value is defined as: 45.0
The product is: 472.5

Example 2

In this example, we will multiply two floating-point numbers:

fun product(val1 : Float, val2 : Float){ val product = val1 * val2 println("The product is: $product") } fun main() { val val1 = 12.5f val val2 = 40.0f println("The first value is defined as: $val1") println("The second value is defined as: $val2") product(val1, val2) }

Output

The first value is defined as: 12.5

The second value is defined as: 40.0
The product is: 500.0

Example 3

In this example, we will multiply two floating-point numbers (Double) using the multiplication arithmetic operator

fun main() { val val1 = 12.0 val val2 = 23.5 println("The first value is defined as: $val1") println("The second value is defined as: $val2") val product = val1 * val2 println("
The product is: $product"
) }

Output

The first value is defined as: 12.0

The second value is defined as: 23.5
The product is: 282.0

Updated on: 13-Oct-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements