Kotlin program to input numbers (integer, float, double) at run time

Kotlin | Input Numbers: Here, we are going to learn how to input various types of numbers and print them in Kotlin? Submitted by IncludeHelp, on April 16, 2020

In any programming language, input-output is important to interact with the user. In Kotlin, we use Scanner for input and println/print for output.

Kotlin - Input numbers (integer, float, double) at run time

Here, we are implementing a Kotlin program to input various types of numbers and printing them on screen.

Program to input and print numbers in Kotlin

package com.includehelp.basic

import java.util.*

// Main Method Entry Point of Program
fun main(args: Array<String>) {
    // InputStream to get Input
    var reader = Scanner(System.`in`)
    
    // Input Integer Value
    println("Enter Integer Value : ")
    val intValue = reader.nextInt()
    println("Integer Number is : $intValue")
    
    // Input Long Value
    println("Enter Long Value : ")
    val longValue = reader.nextLong()
    println("Long Number is : $longValue")
    
    // Input Float Value
    println("Enter Float Value : ")
    val floatValue = reader.nextFloat()
    println("Float Number is : $floatValue")
    
    // Input Double Value
    println("Enter Double Value : ")
    val doubleValue = reader.nextDouble()
    println("Double Number is : $doubleValue")
}

Output

Run 1:
Enter Integer Value :
123
Integer Number is : 123
Enter Long Value :
34355566
Long Number is : 34355566
Enter Float Value :
45.34
Float Number is : 45.34
Enter Double Value :
456.78
Double Number is : 456.78

In the above program, we are taking input of the numbers (integer, long, float and double) using methods of Scanner class. nextInt() is used for Integer value input, nextLong() for Long value input, nextFloat() for Float value input, and nextDouble() for Double value input.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.