Print the Name of Month Based on Entered Number

fun main() {
    println("Enter a Number")//1
    val number = readLine()!!.toInt()//2
    var monthName = when (number) {//3
        1 -> "January"
        2 -> "February"
        3 -> "March"
        4 -> "April"
        5 -> "May"
        6 -> "June"
        7 -> "July"
        8-> "Auguest"
        9-> "September"
        10->"October"
        11->"November"
        12->"December"
        else -> "Sorry Wrong Input"

    }
    print(monthName)//4

}
  1. Instruct the user to enter a number
  2. Convert the user to enter into int and assign to number variable
  3. Create and initialize the value of monthName from when expression result
  4. Print the output on the console

Output

Enter a Number
4
April

Enter a Number
34
Sorry Wrong Input

Leave a Reply

Your email address will not be published. Required fields are marked *