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
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,…