Write a program to check whether an alphabet is vowel or consonant.
To understand this program, you should have knowledge of basic if-else statement. There are 6 Vowels in alphabets namely ‘a’, ‘e’, ‘i’, ‘o’, ‘u’. And other 21 Alphabets are consonant. So, if the entered character is any of 6 defined above, we will print vowel else consonant.
public class VowelOrConsonant { public static void main(String[] args) { char ch = 'o'; if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ) System.out.println(ch + " is a vowel"); else System.out.println(ch + " is a consonant"); } }
Output
o is a vowel
As you know, any java program starts from main method. here, we have assigned alphabet ‘o’ to a char
variable ch.
The if statement checks entered char
is == a. if not, it checks the next condition and likewise. Double pipe operator || is OR symbol.
If it finds a match in if statement, it prints vowel else it prints consonant.
Additionally, if you assign any Capital letter to char
variable, it will print consonant which should not happen.
Assignment: Write a program to include Capital vowels in the program as well.