In Kotlin, it is very simple to check whether an alphabet is a vowel or a consonant.
Before we begin writing the program, let’s define what a vowel and a consonant is.
A vowel is a letter in the alphabet that sounds with an open vocal tract, whereas a consonant is a letter that sounds with a closed or partially closed vocal tract.
Vowels include A, E, I, O, U, and sometimes Y. All other letters of the alphabet are consonants.
Now, let’s write a program in Kotlin to check whether a given alphabet is a vowel or a consonant.
fun main(args: Array<String>) { val alphabet = 'a' if (alphabet == 'a' || alphabet == 'e' || alphabet == 'i' || alphabet == 'o' || alphabet == 'u') { println("$alphabet is a vowel") } else { println("$alphabet is a consonant") } }
In this program, we have declared a variable alphabet with the value of the letter ‘a’.
Then we have used an if-else statement to check if the alphabet is a vowel or a consonant.
We have used the logical OR operator to check whether the alphabet is any of the five vowels.
If it is a vowel, we print the statement “$alphabet is a vowel”, and if it is not a vowel, we print the statement “$alphabet is a consonant”.
We can easily modify this program to check for any alphabet.
We just need to change the value of the alphabet variable to the alphabet we want to check.
fun main(args: Array<String>) { val alphabet = 'c' if (alphabet == 'a' || alphabet == 'e' || alphabet == 'i' || alphabet == 'o' || alphabet == 'u') { println("$alphabet is a vowel") } else { println("$alphabet is a consonant") } }
In this program, we have changed the value of the alphabet variable to ‘c’. When we run this program, it will print the statement “$alphabet is a consonant” because ‘c’ is not a vowel.
In conclusion, checking whether an alphabet is a vowel or a consonant is a simple task in Kotlin.
We just need to use an if-else statement and check whether the alphabet is any of the five vowels using the logical OR operator.