Kotlin Program to Multiply Two Floating Point Numbers

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

In this post, we will go through kotlin program to multiply two floating point numbers

Multiply Two Floating Point Numbers

Let’s see how to calculate product.

We use nextFloat() method to read entered floating point number.

import java.util.*

fun main() {

    val read = Scanner(System.`in`)

    println("Enter first floating number:")
    val a = read.nextFloat()

    println("Enter second floating number:")
    val b = read.nextFloat()

    val result = a * b

    println("Product of $a and $b = $result")
}

When you run the program, output will be

Enter first floating number:
10
Enter second floating number:
21
Product of 10.0 and 21.0 = 210.0
Explanation:

We have stored two floating point numbers entered by user in variables a and b.
Then, we defined another variable result and stored product of two variables a and b.
Finally, result is shown using println()

read.nextFloat() means read floating point number entered by user.

a = read.nextFloat() means read floating point number entered by user and store it in a.

What if you enter some random string that can not be converted into float? Will that program run properly?

Answer – It will throw exception – InputMismatchException.

Let’s take an example, run above program again and enter 12gr when asked to enter first floating point number. Output will be –

Enter first floating number:
12gr
Exception in thread "main" java.util.InputMismatchException
	at java.base/java.util.Scanner.throwFor(Scanner.java:939)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextFloat(Scanner.java:2496)
	//Some other details....

Thus, we went through Kotlin Program to multiply two floating point numbers.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners