The alphabets A, E, I, O and U (small case and upper case) are known as vowels and rest of the alphabets are known as consonants.
Method 1: Program to check if character is vowel or consonant using switch case
import java.util.Scanner; public class VowelOrConsonantExample { public static void main(String[] args) { boolean isVowel = false; Scanner scanner = new Scanner(System.in); System.out.println("Enter a character : "); char ch = scanner.next().charAt(0); scanner.close(); switch (ch) { case 'a': isVowel = true; break; case 'e': isVowel = true; break; case 'i': isVowel = true; break; case 'o': isVowel = true; break; case 'u': isVowel = true; break; case 'A': isVowel = true; break; case 'E': isVowel = true; break; case 'I': isVowel = true; break; case 'O': isVowel = true; break; case 'U': isVowel = true; } if (isVowel == true) { System.out.println(ch + " is a Vowel"); } else { if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) System.out.println(ch + " is a Consonant"); else System.out.println("Input is not an alphabet"); } } }
Output
Enter a character :
A
A is a Vowel
Enter a character :
h
h is a Consonant
Method 2: Program to check if character is vowel or consonant using using if..else statement
import java.util.Scanner; public class VowelOrConsonantExample { public static void main(String[] args) { int i = 0; Scanner sc = new Scanner(System.in); System.out.println("Enter a character : "); char ch = sc.next().charAt(0); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { System.out.println("Entered character " + ch + " is Vowel"); } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) System.out.println("Entered character " + ch + " is Consonant"); else System.out.println("Not an alphabet"); } }
Output
Enter a character :
I
Entered character I is Vowel
Enter a character :
V
Entered character V is Consonant
Enter a character :
;
Not an alphabet