In this you learn to create Java Program to Check Whether an Alphabet is Vowel or Consonant using if..else statement and switch statement.
Check whether an alphabet is vowel or consonant using if..else statement
public class VowelConsonant {
public static void main(String[] args) {
char ch = 'a';
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
System.out.println(ch + " is vowel");
else
System.out.println(ch + " is consonant");
}
}
This program first prompts the user to enter an alphabet. Then, it uses an if-else statement to check if the entered alphabet is a vowel or a consonant. If the alphabet is a vowel, the program prints ” is a vowel”. Otherwise, the program prints ” is a consonant”.
Output
a is vowel
Check whether an alphabet is vowel or consonant using switch statement
public class VowelConsonant {
public static void main(String[] args) {
char ch = 'z';
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println(ch + " is vowel");
break;
default:
System.out.println(ch + " is consonant");
}
}
}
In the above program, instead of using a long if
condition, we replace it with a switch case
statement.
If ch is either of cases: ('a', 'e', 'i', 'o', 'u')
, vowel is printed. Else, default case is executed and consonant is printed on the screen.
Output
z is consonant
Happy Programming:)