w3resource

Kotlin program: User input and display

Kotlin Basic: Exercise-4 with Solution

Write a Kotlin program to take user input and display it.

Sample Solution:

Kotlin Code:

fun main(args: Array) {
    if (args.isNotEmpty()) {
        val name = args[0]
        println("Entered name: $name")
    } else {
        println("No name provided. Please provide a name as a command-line argument.")
    }
} 

Sample Output:

Entered name: Virginia

Explanation:

In the above exercise, the readLine() function is used to read the user input from the console. The input is then stored in the name variable. Finally, the program displays the entered name using string interpolation.

Kotlin Editor:


Previous: Display current date and time.
Next: Arithmetic operations on two numbers.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.