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
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
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
Suggested for you
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
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…