Java program to check Vowel or consonant using switch case statements
Java program to check Vowel or consonant using switch case
In this article, we will discuss the concept of the Java program to check Vowel or consonant using switch case statements
In this post, we are going to learn how to check the vowels and consonants using switch statements in the Java programming language
Check vowel or consonant using switch case statements with the break
The program allows to enter an Alphabet and it checks and displays whether the given alphabet is vowel or consonant with the break statements
Program 1
import java.util.Scanner; public class CheckVowelSwitchcase{ public static void main(String args[]){ char ch;//variable declaration Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the Alphabet for check vowel or consonant "); ch=scan.next().charAt(0);;// store the input from the user switch(ch){ //check lower case vowel letters case 'a': System.out.println(ch+" is a vowel"); break; case 'e': System.out.println(ch+" is a vowel"); break; case 'i': System.out.println(ch+" is a vowel"); break; case 'o': System.out.println(ch+" is a vowel"); break; case 'u': System.out.println(ch+" is a vowel"); break; //check upper case vowel letters case 'A': System.out.println(ch+" is a vowel"); break; case 'E': System.out.println(ch+" is a vowel"); break; case 'I': System.out.println(ch+" is a vowel"); break; case 'O': System.out.println(ch+" is a vowel"); break; case 'U': System.out.println(ch+" is a vowel"); break; default: System.out.println(ch+" is a consonant"); break; } } }
When the above code is executed, it produces the following result
Case 1
Enter the Alphabet for check vowel or consonant a a is a vowel
Case 2
Enter the Alphabet for check vowel or consonant U U is a vowel
Case 3
Enter the Alphabet for check vowel or consonant k k is a consonant
Case 4
Enter the Alphabet for check vowel or consonant M M is a consonant
Approach
- Define a character variable ch
- The program is asked the user to enter an Alphabets for check vowel or consonant
- Entered character is stored in the ch variable
- Define cases for the character ch with vowel character both capital and small
- Next, the program checks every case using the given Alpabets.
- Finally, it displays whether the given character is a vowel or a consonant
Check Vowel or consonant using switch case statements without the break
The program allows to enter an Alphabet and it checks and displays whether the given alphabet is vowel or consonant without the break statements
Program 2
import java.util.Scanner; public class CheckVowelSwitchcase1{ public static void main(String args[]){ char ch;//variable declaration boolean isVowel=false;//Declare a boolean variable Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.println("Enter the character "); ch=scan.next().charAt(0);;// store the input from the user switch(ch){ //check lower case vowel letters case 'a': case 'e': case 'i': case 'o': case 'u': //check upper case vowel letters case 'A': case 'E': case 'I': case 'O': 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(ch+" is not a Alphabets"); } } } }
When the above code is executed, it produces the following result
Case 1
Enter a character a a is a vowel
Case 2
Enter a character I I is a vowel
Case 3
Enter a character v v is a consonant
Case 4
Enter a character F F is a consonant
Case 5
Enter a character $ $ is not a character
Approach
- Define a character variable ch to check the entered character
- define a boolean variable isVowel and initialized it with false
- The program is asked the user to enter an Alphabet to check whether vowel or consonant
- Entered character is stored in the ch variable
- Define cases for the character ch with vowel character both capital and small
- For all cases, The boolean value is assigned true
- Next, the program checks every case using the given character using if statements.
- Finally, it displays whether the given character is a vowel or a consonant
Suggested for you
Operator in Java
Data type and variable in Java
Similar post
C program to check Vowel or consonant using switch case statements
C++ program to Check Vowel or consonant using switch case statements