In this Java program, you’ll learn how to check if a character is a vowel or consonant using Java with some sample code snippets.Â
How to Check if a Character is a Vowel or Consonant?Â
Example 1:Â
RUN CODE SNIPPETimport java.util.Scanner; public class Main {    public static void main(String args[]) {       System.out.println("Enter a character :");       Scanner sc = new Scanner(System.in);       char ch = sc.next().charAt(0);       if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'||ch == ' ') {          System.out.println("The given character is a vowel");       } Else {          System.out.println("The given character is a consonant");       }    } }
OUTPUTÂ
Enter a character: e Given character is a vowel
The alphabets ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels and the remaining are called consonants.Â
 In the above example, we are using a loop and or operator to find whether the given character is vowels are consonants.Â
Example 2:Â
RUN CODE SNIPPETpublic class Main {Â Â Â Â Â public static void main(String[] args) {Â Â Â Â Â Â Â Â Â char ch = 'r';Â Â Â Â Â Â Â Â Â 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");Â Â Â Â Â Â Â Â Â }Â Â Â Â Â }Â }
OUTPUTÂ
r is consonant
In the above example, we are using the switch to find whether the given character is vowels are consonants.Â