In this article, we will discuss the concept of Program to Check whether an Alphabet is a vowel or consonant in Java programming language
In the English language, ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ are vowels and remaining letters are consonant
In this post, we are going to learn how to find whether and the given alphabet is vowel or consonant
Program 1
The program allows the user to enter an Alphabet thereafter it will check and display the result of the given character whether “it is a vowel” or “consonant” using the if-else statements in Java language
import java.util.Scanner; class Check_Vowel{//class declaration public static void main(String args[]){//main metod declaration char ch;//local variable Scanner scan=new Scanner(System.in); //This statements takes input from theuser System.out.print("Enter an Alphabet: "); //get input and it is stored ch variable ch=scan.next().charAt(0); if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U') { System.out.print(ch+" is vowel"); } else{ System.out.print(ch+" is consonant"); } }//end of mainmethod }
When the above code is executed, it produces the following result
case 1
Enter an Alphabet: a a is vowel
Case 2
Enter an Alphabet: E E is vowel
Case 3
Enter an Alphabet: M M is consonant
Case 4
Enter an Alphabet: b b is consonant
Approach
The program allows the user to enter an Alphabet thereafter it will check and display the result of the given character whether it is a vowel or consonant using the if-else-if else statements in Java language
Program 2
import java.util.Scanner; class Check_Vowel2{//class declaration public static void main(String args[]){ char ch;//local variable Scanner scan=new Scanner(System.in); //This statements takes input from the user System.out.print("Enter an Alphabet: "); //get input and it is stored ch variable ch=scan.next().charAt(0); if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U') {//if statements checks vowels System.out.print(ch+" is vowel"); } else if//else if statement checks consonant ((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')){ System.out.print(ch+" is consonant"); } else{ System.out.print(ch+" is not an alpabet"); } } }
When the above code is executed, it produces the following result
Case 1
Enter an Alphabet: A A is vowel
Case 2
Enter an Alphabet: e e is vowel
Case 3
Enter an Alphabet: H H is consonant
Case 4
Enter an Alphabet: m m is consonant
Case 4
Enter an Alphabet: % % is not an alphabet
Approach
The program allows the user to enter an Alphabet thereafter it will check and display the result of the given character whether it is a vowel or consonant using the Nested if statements in Java language
Program 3
import java.util.Scanner; class Check_Vowel3{//class declaration public static void main(String args[]){ char ch;//local variable Scanner scan=new Scanner(System.in); //This statements takes input from the user System.out.print("Enter an Alphabet: "); //get input and it is stored ch variable ch=scan.next().charAt(0); if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')){ //outer if use to check consonant if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U') {//inner if statements checks vowels System.out.print(ch+" is vowel"); } else{//display consonant System.out.print(ch+" is consonant"); } } else{ System.out.print(ch+" is neither a vowel nor a consonant"); } } }
When the above code is executed, it produces the following result
Case 1
Enter an Alphabet: A A is vowel
Case 2
Enter an Alphabet:e e is vowel
Case 3
Enter an Alphabet : G G is consonant
Case 4
Enter an Alphabet: f f is consonant
Case 5
Enter an Alphabet:& & is neither a vowel nor a consonant
Approach
Suggested for you
Data type and variable in Java
If-else statements in Java Language
Similar post
C code to check whether the Alphabet is vowel or consonant
C++ code to check whether the Alphabet is vowel or consonant
Python code to check whether the Alphabet is vowel or consonant
C code to check whether the character is Alphabet or not
C++ code to check whether the character is Alphabet or not
Java code to check whether the character is Alphabet or not
Python code to check whether the character is Alphabet or not
Java program to print all upper case and lower case Alphabets
C++ program to print all upper case and lower case Alphabets
C program to print all upper case and lower case Alphabets
Java code to print all upper case and lower case Alphabets between the given range
C++ code to print all upper case and lower case Alphabets between the given range
C code to print all upper case and lower case Alphabets between the given range
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…