Q:

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

belongs to collection: Kotlin Basic Programs

0

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.

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Kotlin program to input a string... >>
<< Kotlin program to perform simple calculation on tw...