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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/kotlin-exercises/basic/kotlin-basic-exercise-4.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics