Java Program to check vowel or consonant using switch case
Written by
In the following question, we are supposed to ask the user for character input and check if the input is a vowel or not using Switch Case construct.
Algorithm:
- Accept and store user input.
- Make a copy of the input and store it.
- Convert the copy to uppercase for easier comparison and uniformity.
- Use a switch-case construct to check for vowels.
- Print the result based on the vowel check.
/* Program to check if entered character is vowel or not using Switch Case */
import java.util.Scanner;
class SwitchVowel {
public static void main() {
Scanner inp = new Scanner(System.in);
System.out.print("\nEnter Character: ");
char c = (inp.nextLine()).charAt(0);
char z = Character.toUpperCase(c); // Changing Value to UpperCase for uniformity.
switch (z) { // Checking Vowel Character using Switch Case
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
System.out.println(c + " is a Vowel.");
break;
default:
System.out.println(c + " is a Non-Vowel Character.");
}
}
}
Output:
Enter Character: X
X is a Non-Vowel Character.
Enter Character: u
u is a Vowel.
Enter Character: 5
5 is a Non-Vowel Character.