- On
- By
- 0 Comment
- Categories: Alphabet, Check value
Program to Check whether an Alphabet is vowel or consonant in Java
Program to Check whether an Alphabet is a vowel or consonant in Java
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 to find an Alphabet is a vowel or consonant
Check whether an Alphabet is a vowel or consonant in Java using if-else
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
- In the program, the user is asked to enter an Alphabet and entered Alphabet is stored in character variable ch
- The program evaluates whether the entered Alphabet is a vowel or consonant, using if-else statements
- If the given Alphabet is a vowel The program displays the output “it is a vowel” and if the given Alphabet is not vowel it will display”it is consonant”
Check whether an Alphabet is a vowel or consonant in Java using if-else if-else
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
- In the program, the user is asked to enter an Alphabet and entered Alphabet is stored in character variable ch
- The program evaluates whether the entered Alphabet is a vowel or consonant, using if else-if else statements
- If the given Alphabet is a vowel The program displays the output “it is a vowel” and if the given Alphabet is not vowel it will display”it is consonant”
Check whether an Alphabet is a vowel or consonant in Java using Nested if-else statements
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
- In the program, the user is asked to enter an Alphabet and entered Alphabet is stored in character variable ch
- The program evaluates whether the entered Alphabet is a vowel or consonant, using nested if statements
- If the given Alphabet is a vowel The program displays the output “it is a vowel” and if the given Alphabet is not vowel it will display”it is consonant”
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