Kotlin program to check if an alphabet is vowel or not :
This is a simple Kotlin example to show you how when and if-else condition works in Kotlin. Problem is to find out if an alphabet is vowel or consonent.Our program will print out the result in one statement i.e. if the character is alphabet or vowel. Let’s take a look :
Program :
fun main(args: Array) {
val c = 'x'
when(c) {
'a', 'e', 'i', 'o', 'u' -> println("$c is a vowel")
else -> println("$c is a consonant")
}
}
Output :
x is a consonant
As you can see that the when condition is same as like switch in Java. It checks for multiple cases where the input character c falls and prints out the result accordingly. If you want , you can also use if-else condition like below :
fun main(args: Array) {
val c = 'i'
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){
println("${c} is a vowel")
}else{
println("${c} is a consonant")
}
}
Output :
i is a vowel
You might also like :