Table of Contents
Java program to check whether an Alphabet is Vowel or Consonant
In this article, we will discuss the concept of Java program to check whether an Alphabet is Vowel or Consonant
In this post, we are going to learn how to write a program to check whether given English alphabet is Vowel or consonant using various ways in Java language
Code to check given Alphabet is Vowel or consonant
Code to check given Alphabet Vowel or consonant, using If else
In this code, we are going to learn how to check the given English alphabet is Vowel or consonant using if-else in Java language
Program 1
import java.util.Scanner; class Check_Alpha_Vowel1{//class declaration public static void main(String args[]){ char ch;//Declare a local character variable Scanner scan=new Scanner(System.in); //Ask input from the user System.out.print("Enter an Alphabet: "); //get input and store 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') {//check the given Alphabet is vowel or consonant System.out.print(ch+" is a vowel"); } else{ System.out.print(ch+" is a consonant"); } } }
When the above code is executed, it produces the following result
Case 1
Enter an Alphabet: E E is a Vowel
Case 2
Enter an Alphabet: r r is a Consonant
Code to check given Alphabet Vowel or consonant using If else-if else
In this code, we are going to learn how to check the given English alphabet is Vowel or consonant using if-else-if else in Java language
Program 2
import java.util.Scanner; class Check_Alpha_Vowel2{//class declaration public static void main(String args[]){ char ch;//Declare character (local) variable Scanner scan=new Scanner(System.in); //This statements takes input from the user System.out.print("Enter a character:(Alphabet or Not) "); //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') {//this statements checks vowels System.out.print(ch+" is a vowel"); } else if//"else if" statement checks consonant ((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')){ System.out.print(ch+" is a consonant"); } else{ System.out.print(ch+" is not an Alphabet"); }//print non-alphabet characters } }
When the above code is executed, it produces the following result
Case 1
Enter a character:(Alphabet or Not) U U is a Vowel
Case 2
Enter a character:(Alphabet or Not) t t is a Consonant
Case 3
Enter a character:(Alphabet or Not) 5 5 is not an Alphabet
Code to check given Alphabet Vowel or consonant using Nested-If
In this code, we are going to learn how to check the given English alphabet is Vowel or consonant using Nested if in Java language
Program 3
import java.util.Scanner; class Check_Alpha_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 a character: "); //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 a vowel"); } else{//display consonant System.out.print(ch+" is a consonant"); } } else{ System.out.print(ch+" is not an Alphabet"); } } }
When the above code is executed, it produces the following result
Case 1
Enter a Alphabet: F F is a Consonant
Case 2
Enter a Alphabet: I I is a Vowel
Case 3
Enter a Alphabet: & & is not an Alphabet
Code to check given Alphabet Vowel or not using switch case
In this code, we are going to learn how to check the given English alphabet is Vowel or consonant using Switch case statements in Java language
Program 4
import java.util.Scanner; public class CheckVowConsSwitchcase1{ 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 in ch variable from the user switch(ch){ //check lower case vowel letters case 'a': System.out.println(ch+" is an vowel"); break; case 'e': System.out.println(ch+" is an vowel"); break; case 'i': System.out.println(ch+" is an vowel"); break; case 'o': System.out.println(ch+" is an vowel"); break; case 'u': System.out.println(ch+" is an vowel"); break; //check upper case vowel letters case 'A': System.out.println(ch+" is an vowel"); break; case 'E': System.out.println(ch+" is an vowel"); break; case 'I': System.out.println(ch+" is an vowel"); break; case 'O': System.out.println(ch+" is an 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 E E is a vowel
Case 2
Enter the Alphabet for check vowel or consonant g g is a consonant
Suggested post
Nested if statements in Java language
Do-while loop in Java language
Similar post
Java program to print all alphabets in given range
C program to print all alphabets in given range
C++ program to print all alphabets in given range
Java program to print all alphabets using loops
C program to print all alphabets using loops
C++ program to print all alphabets using loops
Java program to check whether a character is Alphabet or Not
C++ program to check whether a character is Alphabet or Not
C program to check whether a character is Alphabet or Not
Python program to check whether a character is Alphabet or Not